Search Terms
arrow function type guard
Suggestion
Specific simple cases of arrow functions of a single argument should be considered type guards:
constf1=x=>xtypeof"string";// immediately returns a typeof expression with a literal rhsconstf2=x=>xinstanceofFoo;// immediately returns an instanceof expressionconstf3=x=>x==="foo";// immediately returns a strict (non-)equality comparison with a literalconstf4=x=>x==null;// immediately returns a non-strict (non-)equality comparison with a null literalconstf5=x=>isFoo(x);// immediately returns another type guard
There are more general requests like #16069, but I beleive this one should be easier to implement, and it still covers a lot of use cases.
Use Cases
filter with heterogenous arrrays:
// currently: (string | number)[]// would be: string[]consta=[1,"foo",2,"bar"].filter(x=>xinstanceof"string");
Specifically, filtering out null or undefined elements:
// f: (x: string) => Promise<string | undefined>consta=awaitPromise.all(["foo","bar"].map(f));constb=a.filter(x=>x!==undefined);
Limiting the number of arguments of a more complex type guard:
// isFoo: (x: any, strict?: boolean) => x is Fooconstb1=a.filter(x=>isFoo(x));constb2=a.filter(x=>isFoo(x,true));// compare:constb3=a.filter(isFoo);// No overload matches this call.
Examples
Checklist
My suggestion meets these guidelines:
Search Terms
arrow function type guard
Suggestion
Specific simple cases of arrow functions of a single argument should be considered type guards:
There are more general requests like #16069, but I beleive this one should be easier to implement, and it still covers a lot of use cases.
Use Cases
filterwith heterogenous arrrays:Specifically,
filtering outnullorundefinedelements:Limiting the number of arguments of a more complex type guard:
Examples
Checklist
My suggestion meets these guidelines: