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
using infer to extract the type of a generic which is passed to a conditional type ends up reporting error Argument of type 'true' is not assignable to parameter of type 'never'.(2345) but only for boolean.
The returned type is a union FactoryReturnAFn<false> | FactoryReturnAFn<true> instead of FactoryReturnAFn<boolean>;
passing then true or false as parameter leads to compile error.
interfaceBaseType<T=undefined>{readonlydata: T;}// infered typestypeExtractType<T>=TextendsBaseType<infer A> ? A : undefined;// define type definitions for function signatures that the factory can returntypeFactoryReturnAFn<T>=(p: T)=>BaseType<T>;typeFactoryReturnBFn<T>=(p: T)=>BaseType<number>;// define a conditional typetypeConditionalFactoryReturnFn<T>=Textendsundefined
? FactoryReturnBFn<T>
: FactoryReturnAFn<T>;functionfactory<TextendsBaseType<ExtractType<T>>>(): ConditionalFactoryReturnFn<ExtractType<T>>{return((p: ExtractType<T>): BaseType<ExtractType<T>>=>{return{data: p};})asunknownasConditionalFactoryReturnFn<ExtractType<T>>;}// specify a concrete type derived from BaseType, with different generic types except boolean// number, string, string[], { readonly id: string } etc.typeSpecificType=BaseType<{readonlyid: string}>;constcallbackWithTypes=factory<SpecificType>();callbackWithTypes({id: "string"});// specify a concrete type derived from BaseType, with booleantypeSpecificTypeBoolean=BaseType<boolean>;// the callback signature returned from the factory becomes a union type // and passing true | false tsc error// Argument of type 'true' is not assignable to parameter of type 'never'.(2345)constcallbackWithBoolean=factory<SpecificTypeBoolean>();callbackWithBoolean(true);// passing true or false is no possible// expected signature// callbackWithBoolean === FactoryReturnAFn<boolean>// actual signature// callbackWithBoolean === FactoryReturnAFn<false> | FactoryReturnAFn<true>
Expected behavior:
type defintion to be properly using boolean type FactoryReturnAFn<boolean>
Actual behavior:
type definition is union type FactoryReturnAFn<false> | FactoryReturnAFn<true> and blocks to call fun ction with true or false as parameter.
@RyanCavanaugh thanks for the answer after playing a bit around I have got it what to do, was for me not that obvious. I post the solution here.
interfaceBaseType<T=undefined>{readonlydata: T;}// infered typestypeExtractType<T>=TextendsBaseType<infer A> ? A : undefined;// define type definitions for function signatures that the factory can returntypeFactoryReturnAFn<T>=(p: T)=>BaseType<T>;typeFactoryReturnBFn<T>=(p: T)=>BaseType<number>;// define a conditional type, FIX: [T]typeConditionalFactoryReturnFn<T>=[T]extendsundefined
? FactoryReturnBFn<T>
: FactoryReturnAFn<T>;functionfactory<TextendsBaseType<ExtractType<T>>>(): ConditionalFactoryReturnFn<ExtractType<T>>{return((p: ExtractType<T>): BaseType<ExtractType<T>>=>{return{data: p};})asunknownasConditionalFactoryReturnFn<ExtractType<T>>;}typeSpecificType=BaseType<{readonlyid: string}>;constcallbackWithTypes=factory<SpecificType>();callbackWithTypes({id: "string"});typeSpecificTypeBoolean=BaseType<boolean>;constcallbackWithBoolean=factory<SpecificTypeBoolean>();callbackWithBoolean(true);typeMyUnion="a"|true|100|string[];typeSpecificTypeUnion=BaseType<MyUnion>;constcallbackWithUnion=factory<SpecificTypeUnion>();callbackWithUnion(true);callbackWithUnion("a");callbackWithUnion(100);callbackWithUnion(["works"]);callbackWithUnion(false);
Dear TS Team,
using infer to extract the type of a generic which is passed to a conditional type ends up reporting error
Argument of type 'true' is not assignable to parameter of type 'never'.(2345) but only for boolean.
The returned type is a union
FactoryReturnAFn<false> | FactoryReturnAFn<true>
instead ofFactoryReturnAFn<boolean>;
passing then true or false as parameter leads to compile error.
TypeScript Version: 3.7.x, 3.8.x, nighly
Search Terms: infer, conditional, generic, boolean
Code
Expected behavior:
type defintion to be properly using boolean type
FactoryReturnAFn<boolean>
Actual behavior:
type definition is union type
FactoryReturnAFn<false> | FactoryReturnAFn<true>
and blocks to call fun ction with true or false as parameter.Playground Link: Example
Related Issues:
#33369
The text was updated successfully, but these errors were encountered: