Skip to content

Symbol is incompatible with index signature #21962

Closed
@DeTeam

Description

@DeTeam

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:

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