Skip to content
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

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

Closed
yortus opened this issue Nov 20, 2017 · 0 comments · Fixed by #21217
Closed

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

yortus opened this issue Nov 20, 2017 · 0 comments · Fixed by #21217
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@yortus
Copy link
Contributor

yortus commented Nov 20, 2017

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.

@mhegazy mhegazy added the Bug A bug in TypeScript label Nov 20, 2017
@mhegazy mhegazy added this to the TypeScript 2.7 milestone Nov 20, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 17, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants