Open
Description
Bug Report
{ [prop]() { } }[prop]
fails with Type 'symbol' cannot be used as an index type.
aka ts(2538)
if prop: string | symbol
. It works fine if prop
is either string
or symbol
.
🔎 Search Terms
union type cannot be used as an index type
Found 2 somewhat relevant issues and a pull request, but they don't seem to directly apply. Please close if duplicate:
- Index signatures for symbols and template literal strings #44512
- Quick fix for 'unions can't be used in index signatures, use a mapped object type instead' #24220
Type 'string' cannot be used to index type 'T'
when indexing a generic function parameter #47357
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about union and index types
⏯ Playground Link
Playground link with relevant code
💻 Code
declare function assert(condition: any, msg?: string): asserts condition;
function wrap(x: any) {
return new Proxy(x, {
get(_target, p, receiver) {
return {
[p](...args: any[]) {
assert(this === receiver);
return Reflect.apply(x[p], x, args);
},
}[p]
},
has() {
return true;
}
});
}
declare const prop: string | symbol;
const shortHandFn = { [prop]() { } }[prop];
🙁 Actual behavior
Type 'symbol' cannot be used as an index type.
🙂 Expected behavior
No type error since both symbol and string are allowed index types.