You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Push the static flow analysis a notch and have inferred return types for conditional expressions and statements
Use Cases
At the moment the only way to have a return type inferred from a conditional flow is to recreate the same conditional structure with a conditional type in a separate type and then cast the result when needed, this may lead to inconsistencies in the types given that the cast is forced and it bypasses some of the static TS analysis
Examples
// Extremely simple exampleconstres=typeof10=="number" ? 10 : "10";// inferred 10|"10" but actual can only be 10// More convoluted exampleconstmakeObject=(num: number)=>({number: num});constgetOption=<Textends1|2|3|object>(input: T)=>{if(typeofinput==="object")returnnull;switch(input){case1: return"ONE";case2: returnmakeObject(2);case3: return3;}thrownewError();}typeInferReturnType<Textends1|2|3|object>=Textends1 ? "ONE"
: Textends2 ? ReturnType<typeofmakeObject>
: Textends3 ? 3
: Textendsobject ? null
: neverconstgetOptionInfer=<Textends1|2|3|object>(input: T) : InferReturnType<T>=>{if(typeofinput==="object")returnnullasInferReturnType<T>;switch(input){case1: return"ONE"asInferReturnType<T>;case2: returnmakeObject(2)asInferReturnType<T>;case3: return3asInferReturnType<T>;}thrownewError();}constres=getOption(2);// type of res is: 3 | "ONE" | {number: number;} | nullconstresInfer=getOptionInfer(2);// type of resInfer is {number: number;}letgenericInput: 1|3=/* coming from somewhere else */;constres2=getOption(2);// type of res2 is: 3 | "ONE" | {number: number;} | nullconstresInfer2=getOptionInfer(genericInput);// type of resInfer2 is "ONE" | {number: number;}// Expected: to have res and res2 with the same type of resInfer and resInfer2 respectively
Checklist
My suggestion meets these guidelines:
[v] This wouldn't be a breaking change in existing TypeScript/JavaScript code
[v] This wouldn't change the runtime behavior of existing JavaScript code
[v] This could be implemented without emitting different JS based on the types of the expressions
[v] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
Search Terms
Build time conditional flow path type inference
Suggestion
Push the static flow analysis a notch and have inferred return types for conditional expressions and statements
Use Cases
At the moment the only way to have a return type inferred from a conditional flow is to recreate the same conditional structure with a conditional type in a separate type and then cast the result when needed, this may lead to inconsistencies in the types given that the cast is forced and it bypasses some of the static TS analysis
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: