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
{{ message }}
Repository navigation
Generics; ReturnType<Function> not assignable to ReturnType<Function & (conditional type)> #31602
TypeScript Version: TS Playground version (3.4.1), 3.5.0-dev.20190523
Search Terms: ReturnType, function, conditional type
Code
declarefunctiondoThing<Fextends()=>any>(f: ()=>ReturnType<F>): void;//== This fails ==typeBar<Fextends()=>any>=(&(()=>ReturnType<F>)//Should be the same as `unknown`&(Fextendsany ? unknown : unknown));functionbar<Fextends()=>any>(f: F){/* Fail Type 'ReturnType<F>' is not assignable to type 'ReturnType<(() => ReturnType<F>) & (F extends any ? unknown : unknown)>' Type '() => any' is not assignable to type 'F extends any ? unknown : unknown' */doThing<Bar<F>>(() : ReturnType<F>=>{returnf();});//Fail//Type '() => any' is not assignable to type 'F extends any ? unknown : unknown'constt: (Fextendsany ? unknown : unknown)=((): any=>3);//OK!constt2: (unknown)=((): any=>3);}//== This works ==typeBar2<Fextends()=>any>=(&(()=>ReturnType<F>)&unknown);functionbar2<Fextends()=>any>(f: F){//OK!doThing<Bar2<F>>(() : ReturnType<F>=>{returnf();});}
changed the title [-]ReturnType<Function> not assignable to ReturnType<Function & (conditional type)>[/-][+]Generics; ReturnType<Function> not assignable to ReturnType<Function & (conditional type)>[/+]on May 26, 2019
Shorter repro, that shows it also fails for string assignments,
functionbar<F>(f: F){//Fail//Type '() => any' is not assignable to type 'F extends any ? unknown : unknown'constt: (Fextendsany ? unknown : unknown)=((): any=>3);//OK!constt2: (unknown)=((): any=>3);//Fail//Type 'string' is not assignable to type 'F extends any ? string : string'.conste: (Fextendsany ? string : string)=("test"asstring);//Fail//Type 'string' is not assignable to type 'F extends any ? string : string'.conste2: (Extract<Fextendsany ? string : string,string>)=("test"asstring);//OK!conste3: (string)=("test"asstring);}
For generics, I feel like if the true and false branches of a conditional type both extend some type T, the conditional type itself should also extend T.
For generics, I feel like if the true and false branches of a conditional type both extend some type T, the conditional type itself should also extend T.
Activity
[-]ReturnType<Function> not assignable to ReturnType<Function & (conditional type)>[/-][+]Generics; ReturnType<Function> not assignable to ReturnType<Function & (conditional type)>[/+]AnyhowStep commentedon May 26, 2019
Shorter repro, that shows it also fails for string assignments,
Playground
AnyhowStep commentedon May 26, 2019
For generics, I feel like if the true and false branches of a conditional type both extend some type
T, the conditional type itself should also extendT.I'm unsure how unfeasible it is, though.
jack-williams commentedon May 26, 2019
See #26933
AnyhowStep commentedon May 26, 2019
Ah. I see there's a PR open for it already. I guess I can close this, and wait patiently for #30639 or some successor to get merged.