You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Symbol is incompatible with index signature #21962
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
exportconstKEY=Symbol('KEY')exportinterfaceTree{[KEY]?: string[name: string]: Tree}// Here I get a type problemexportconsttree: 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:
I think #21217 was meant to fix this, but this shows there is still a problem. Simpler repro:
exportconstSYM=Symbol('a unique symbol');exportinterfaceI{[SYM]: 'sym';[x: string]: 'str';}leta: I={[SYM]: 'sym'};// ERROR: 'sym' is not assignable to 'str'letb: I={[SYM]: 'str'};// ERROR: 'str' is not assignable to 'sym'
indexTypesRelatedTo inside isRelatedTo doesn't filter out symbol-keyed properties for its eachPropertyRelatedTo check, which is the actual root issue for this one.
Activity
yortus commentedon Feb 16, 2018
I think #21217 was meant to fix this, but this shows there is still a problem. Simpler repro:
mhegazy commentedon Mar 8, 2018
Wesley Wigham (@weswigham) i would have expected this to be fixed by #22339, but does not seem to be the case..
weswigham commentedon Mar 8, 2018
indexTypesRelatedToinsideisRelatedTodoesn't filter outsymbol-keyed properties for itseachPropertyRelatedTocheck, which is the actual root issue for this one.