Skip to content

Unexpected Compilation Error - potential clash with Function.name? #26587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mcclaskc opened this issue Aug 21, 2018 · 2 comments
Closed

Unexpected Compilation Error - potential clash with Function.name? #26587

mcclaskc opened this issue Aug 21, 2018 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@mcclaskc
Copy link

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:

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Aug 21, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.2 milestone Aug 21, 2018
@ghost
Copy link

ghost commented Aug 22, 2018

Repro:

interface I { x: number; }
interface Idx { [index: string]: U; }
type U = Idx | I | "lit";
const u: U = { x: "lit" };

No repro if using string instead of "lit".

@sheetalkamat
Copy link
Member

sheetalkamat commented Oct 11, 2018

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 }.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants