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
Running into what I think is a bug with type discrimination of a function when you have a mapped type on a generic type . I've tried to make the reproduction as simple as possible.
TypeScript Version: 4.1.0-beta
Search Terms:
discrimination, generic, mapped type, strictNullChecks off
Code
typeMapping<T>={[keyinkeyofT]?: string|(()=>string)};functionfoo<T>(mapping: Mapping<T>){constresults: {[key: string]: string}={};for(constkeyinmapping){constvalueTransform=mapping[key];if(typeofvalueTransform==='function'){results[key]=valueTransform();//! This expression is not callable...}elseif(typeofvalueTransform==='string'){results[key]=valueTransform;}}returnresults;}
Expected behavior:
Actual behavior: strictNullChecks:false I get the following error:
This expression is not callable.
Not all constituents of type 'string | (() => string)' are callable.
Type 'string' has no call signatures.
If you change it back to true, it will work as expected. It seems to have something to do with the combination of generics & mapped types, as if you change to reference Foo directly the code will compile.
type Mapping<T> = { [key in keyof Foo]?: string | (() => string) };
Also the discrimination only seems to break on the typeof valueTransform === 'function'
@web-padawan I would argue in the generic case the insatnceof behavior is wrong and this should be an error. If T is (a: string) => string so the type guard does not exclude T from the true branch:
typeA0<A0T>=Exclude<A0T,Function>;typeA1<A1T>=Exclude<A1T,Function>|Function;functionf<XF>(x: XF): void{typeAF0=A0<XF>typeAF1=A1<XF>consty0={}asAF0;consty1={}asAF1;constz0=typeofy0==='function' ? y0() : undefined;constz1=typeofy1==='function' ? y1() : undefined;/* ^^^^^ This expression is not callable. No constituent of type 'Function | (Exclude<XF, Function> & Function)' is callable.(2349) */constr=typeofy1==='function' ? y1 : nullasnever;typeR=typeofr;// type R = Function}
Uh oh!
There was an error while loading. Please reload this page.
Running into what I think is a bug with type discrimination of a function when you have a mapped type on a generic type . I've tried to make the reproduction as simple as possible.
TypeScript Version: 4.1.0-beta
Search Terms:
discrimination, generic, mapped type, strictNullChecks off
Code
Expected behavior:
Actual behavior:
strictNullChecks:false
I get the following error:If you change it back to
true
, it will work as expected. It seems to have something to do with the combination of generics & mapped types, as if you change to referenceFoo
directly the code will compile.type Mapping<T> = { [key in keyof Foo]?: string | (() => string) };
Also the discrimination only seems to break on the
typeof valueTransform === 'function'
Playground Link: Playground Link
Related Issues:
#27470
The text was updated successfully, but these errors were encountered: