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
Unexpected Compilation Error - potential clash with Function.name? #26587
typeMyObject={[index:string]: MyObject|Function|"wtf"}letmyObj: MyObject;// compilesmyObj={name: "wtf",};// compiles - note the nested key is `notName:`, instead of `name:`myObj={wrapper: {notName: "wtf",},};// as soon as I change the nested key to `name:`, it fails to compile:/*Type '{ name: string; }' is not assignable to type 'Function | MyObject | "wtf"'. Type '{ name: string; }' is not assignable to type '"wtf"'*/myObj={wrapper: {name: "wtf",},};// Other weirdness, if I remove `Function` from the union, it compiles:typeMyObjectNoFunction={[index:string]: MyObjectNoFunction|"wtf"}letmyObjNoFn: MyObjectNoFunction={wrapper: {name: "wtf"}};// More weirdness, if I don't include es6 in my tsconfig, everything compiles
Maybe this is because Function has a name property in es6, so TypeScript infers that { name: "something" } is an unfinished Function type declaration -thedayturns
Playground Link: .
I couldn't get it to happen in playground, but here's a full example on github
The issue here is that string index signature in Idx does not participate in contextual type of property x and thus { x: "lit" } gets widened to { x: string }.
Activity
sheetalkamat commentedon Oct 11, 2018
The issue here is that string index signature in
Idxdoes not participate in contextual type of property x and thus{ x: "lit" }gets widened to{ x: string }.Use string/number signature to get contextual type