Suggestion
🔍 Search Terms
explicit type, assertion, CFA
✅ Viability Checklist
⭐ Suggestion
As stated in #32695, functions using asserts require explicitly typed/annotated like so:
declareletx: unknown;constaFoo=a.literal("foo");aFoo(x);// error// Assertions require every name in the call target to be declared with an explicit type annotation.(2775)// input.ts(2, 7): 'aFoo' needs an explicit type annotation.const_aBar=a.literal("bar")constaBar: typeof_aBar=_aBar;aBar(x);// workslettest1: "bar"=xdeclarelety: unknown;a.string(y)// workslettest2: string=y;namespacea{exportfunctionliteral<T>(t: InferLiteral<T>){returnfunction(v: unknown): asserts v is T{if(v!==t)thrownewError()}}exportfunctionstring(v: unknown): asserts v is string{if(typeofv!=="string")thrownewError();}}typeInferLiteral<T>=|(Textendsstring ? T : string)|(Textendsnumber ? T : number)|(Textendsboolean ? T : boolean)The reason stated in the PR is "This particular rule exists so that control flow analysis of potential assertion calls doesn't circularly trigger further analysis."
My question is, umm, what does this mean in more layman terms? My impression was it's to not allow recursive usage but looks like that's not the case because this compiles... So not sure what the above statement means
namespace a {
export function literal<T>(t: InferLiteral<T>){
return function(v: unknown): asserts v is T {
+ let _aLol = a.literal("lol")+ let aLol: typeof _aLol = _aLol;+ let x = {} as unknown;+ aLol(x)+ let test: "lol" = x;
if (v !== t) throw new Error()
}
}Also I understand it's a "design limitation" but, excuse my ignorance, is it really that hard to lift it? Because it's quite annoying to make make two variables for the same function then annotated the other, makes me think "Eh why can't the compiler do this for itself" haha. Maybe lift the restriction in some scenarios?
📃 Motivating Example
💻 Use Cases
I was writing a fail-fast parser that basically composes assertion functions something like this... But I have to use the mentioned workaround. Even if this is too much of a feature request, an explanation why the requirement exists would make me feel less annoyed when I redeclare and annotate assertions :P
Playground (Hit on run to see the ParseError with message "At Person.age: Expected a number")
namespacea{exportconststring: Asserter<string>=(v,p)=>invariant(typeofv==="string","Expected a string",v,p)exportconstnumber: Asserter<number>=(v,p)=>invariant(typeofv==="number","Expected a number",v,p)exportconstobject=<Oextends{[_instring]: Asserter}>(tO: O):
Asserter<{[KinkeyofO]: O[K]extendsAsserter<infer T> ? T : never}>=>(v,p)=>{invariant((v): v is object=>typeofv==="object","Expected an object",v,p);for(letkintO){(tO[k]asany)((vasany)[k],`${p}.${k}`)}}exportclassParseErrorextendsError{constructor(message: string,publicactual: unknown,publicpath: string){super(`At ${path}: ${message}`)}}functioninvariant(test: boolean,message: string,actual: unknown,path: string): voidfunctioninvariant<T,UextendsT>(test: (v: T)=>v is U,message: string,actual: T,path: string): asserts actual is Ufunctioninvariant<T>(test: (v: T)=>boolean,message: string,actual: T,path: string): voidfunctioninvariant(test: boolean|((v: unknown)=>boolean),message: string,actual: unknown,path: string){if(typeoftest==="function" ? !test(actual) : !test)thrownewParseError(message,actual,path);}}typeAsserter<T=unknown>=(v: unknown,path: string)=> asserts v is Tconst_aPerson=a.object({name: a.string,age: a.number})constaPerson: typeof_aPerson=_aPerson;letperson={name: "Devansh",age: true}asunknown;aPerson(person,"Person");lettest: string=person.name
Suggestion
🔍 Search Terms
explicit type, assertion, CFA
✅ Viability Checklist
⭐ Suggestion
As stated in #32695, functions using
assertsrequire explicitly typed/annotated like so:The reason stated in the PR is "This particular rule exists so that control flow analysis of potential assertion calls doesn't circularly trigger further analysis."
My question is, umm, what does this mean in more layman terms? My impression was it's to not allow recursive usage but looks like that's not the case because this compiles... So not sure what the above statement means
namespace a { export function literal<T>(t: InferLiteral<T>){ return function(v: unknown): asserts v is T { + let _aLol = a.literal("lol")+ let aLol: typeof _aLol = _aLol;+ let x = {} as unknown;+ aLol(x)+ let test: "lol" = x; if (v !== t) throw new Error() } }Also I understand it's a "design limitation" but, excuse my ignorance, is it really that hard to lift it? Because it's quite annoying to make make two variables for the same function then annotated the other, makes me think "Eh why can't the compiler do this for itself" haha. Maybe lift the restriction in some scenarios?
📃 Motivating Example
💻 Use Cases
I was writing a fail-fast parser that basically composes assertion functions something like this... But I have to use the mentioned workaround. Even if this is too much of a feature request, an explanation why the requirement exists would make me feel less annoyed when I redeclare and annotate assertions :P
Playground (Hit on run to see the
ParseErrorwith message"At Person.age: Expected a number")