Closed
Description
TypeScript Version: 3.0.1
Search Terms: typescript compilation function name key union es6 compile error
Code
full example on github .
wtf.ts
type MyObject = {
[index:string]: MyObject | Function | "wtf"
}
let myObj: MyObject;
// compiles
myObj = {
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:
type MyObjectNoFunction = {
[index:string]: MyObjectNoFunction | "wtf"
}
let myObjNoFn: MyObjectNoFunction = {
wrapper: {
name: "wtf"
}
};
// More weirdness, if I don't include es6 in my tsconfig, everything compiles
tsconfig.json
{
"compilerOptions": {
"lib": [
"es6"
],
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"strict": true
}
}
Expected behavior:
no compilation errors
Actual behavior:
compilation error:
Type '{ name: string; }' is not assignable to type 'Function | MyObject | "wtf"'.
Type '{ name: string; }' is not assignable to type '"wtf"'
Corresponding stackoverflow:
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
Related Issues: