Skip to content

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

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:

Activity

  1. sheetalkamat commented on Oct 11, 2018

    @sheetalkamat
    Member

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

  2. added a commit that references this issue on Oct 11, 2018
    c0729a2
  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions