Skip to content

Property '[mySymbol]' is not assignable to string index type #20146

Closed
@yortus

Description

@yortus

TypeScript Version: 2.7.0-dev.20171118

Code

I was expecting this to work after #15473 was merged:

const mySymbol = Symbol('My Symbol');

interface TypeA {
    [mySymbol]: Function;  // OK
    [x: number]: number;
}

interface TypeB {
    [mySymbol]: Function;  // ERROR: Property '[mySymbol]' is not assignable to string index type
    [x: string]: string;
}

let a: TypeA, b: TypeB;
let a1 = a[mySymbol];   // a1 has type Function
let a2 = a[42];         // a2 has type number
let b1 = b[mySymbol];   // b1 has type Function
let b2 = b['foo'];      // b2 has type string

Expected behavior:

No compiler errors. mySymbol is a unique symbol as per #15473 and should not overlap either numeric index types or string index types.

Actual behavior:

Error in TypeB as shown in the code above. TSC rejects the unique symbol key because it is not compatible with the string index type, even though it is not a string property key.

Note that the types of a1, a2, b1 and b2 are all inferred correctly despite the compile error.

Metadata

Metadata

Assignees

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