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:
🕗 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.
Bug Report
{ [prop]() { } }[prop]fails withType 'symbol' cannot be used as an index type.akats(2538)ifprop: string | symbol. It works fine ifpropis eitherstringorsymbol.🔎 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:
Type 'string' cannot be used to index type 'T'when indexing a generic function parameter #47357🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 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.