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

Symbol is incompatible with index signature #21962

Closed
DeTeam opened this issue Feb 15, 2018 · 3 comments
Closed

Symbol is incompatible with index signature #21962

DeTeam opened this issue Feb 15, 2018 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@DeTeam
Copy link

DeTeam commented Feb 15, 2018

I'm trying to use both the index signature in my interface and some symbol fields. They don't seem to work very well together.

Code

export const KEY = Symbol('KEY')

export interface Tree {
  [KEY]?: string
  [name: string]: Tree
}

// Here I get a type problem
export const tree: Tree = {
  'a': {
    [KEY]: 'value1',

    'x': {
      [KEY]: 'value2'
    }
  },
  'b': {
    [KEY]: 'value3'
  },
  'c': {
    [KEY]: 'value4'
  }
}

Expected behavior:

I think there're two options here:

  • Either complain about things on the type level (like if one'd use key (just a string field name) instead of a symbol [KEY])
  • Or allow such usage together with access later const s: Tree = tree['c']; const s: string = tree['c'][KEY]

Actual behavior:

Property '[KEY]' is incompatible with index signature.

Playground Link:

Related Issues:

I think that maybe the following ones are related in a way:

@mhegazy mhegazy added the Bug A bug in TypeScript label Feb 15, 2018
@mhegazy mhegazy added this to the TypeScript 2.8 milestone Feb 15, 2018
@yortus
Copy link
Contributor

yortus commented Feb 16, 2018

I think #21217 was meant to fix this, but this shows there is still a problem. Simpler repro:

export const SYM = Symbol('a unique symbol');

export interface I {
  [SYM]: 'sym';
  [x: string]: 'str';
}

let a: I = {[SYM]: 'sym'}; // ERROR: 'sym' is not assignable to 'str'
let b: I = {[SYM]: 'str'}; // ERROR: 'str' is not assignable to 'sym'

@mhegazy
Copy link
Contributor

mhegazy commented Mar 8, 2018

@weswigham i would have expected this to be fixed by #22339, but does not seem to be the case..

@mhegazy mhegazy assigned weswigham and unassigned rbuckton Mar 8, 2018
@weswigham
Copy link
Member

weswigham commented Mar 8, 2018

indexTypesRelatedTo inside isRelatedTo doesn't filter out symbol-keyed properties for its eachPropertyRelatedTo check, which is the actual root issue for this one.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Mar 8, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 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

No branches or pull requests

5 participants