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
typeExample1<T>=()=>T;typeExample2<T>=Textendsany ? ()=>T : never;typeExample3<T>=Textendsnever ? never : ()=>T;functiontest1<T>(e: Example1<T>): void{}functiontest2<T>(e: Example2<T>): void{}functiontest3<T>(e: Example3<T>): void{}test1(()=>1);// oktest2(()=>1);// oktest3(()=>1);// oktest1(()=>'');// oktest2(()=>'');// oktest3(()=>'');// oktest1(()=>true);// oktest2(()=>true);// failtest3(()=>true);// failtest3(()=>false);// failtest3(()=>trueastrue);// ok// root cause: with boolean, conditional type is inferred to be:typeFalseOrTrueReturn=(()=>false)|(()=>true)constfalseOrTrueReturn: FalseOrTrueReturn=()=>(falseasboolean)// fail// it only fails for function return type, so this works:typeFalseOrTrueArg=((value: false)=>void)|((value: true)=>void)constfalseOrTrueArg: FalseOrTrueArg=(value: boolean)=>{}// ok
Expected behavior:
All failing lines should compile just fine. This is a simplified example, but it prevents correct type inference for more complex real-world use-cases.
Actual behavior:
Compiler fails to compile the lines with a function returning a boolean. This is due to the boolean return value being destructured into () => true | () => false, and () => boolean is not assignable to that.
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.
TypeScript Version: 3.8.0-dev.20200206
Search Terms:
conditional boolean
Code
Expected behavior:
All failing lines should compile just fine. This is a simplified example, but it prevents correct type inference for more complex real-world use-cases.
Actual behavior:
Compiler fails to compile the lines with a function returning a boolean. This is due to the boolean return value being destructured into
() => true | () => false
, and() => boolean
is not assignable to that.Playground Link:
TS Playground
Related Issues:
Possibly:
#33369
#35861
The text was updated successfully, but these errors were encountered: