Open mhofman opened 1 year ago
ts(2538) ts2538 Also ran into this
function makeFunctionWithName (name: PropertyKey) {
return {
[name]: () => undefined
}[name]
}
I'm still getting this error, is there any workarounds? I'm using Vue and the inject/provide syntax uses symbols for accessing stored values.
interface InjectionKey<T> extends Symbol {}
const provides = { }
function provide<T>(key: InjectionKey<T>, value: T) {
provides[key] = value
}
function inject<T>(key: InjectionKey<T>): T {
return provides[key]
}
@mhofman
I hope, this solution will help
const shortHandFn = ({ [prop]() {} } as T)[prop];
// biome-ignore lint:
type Fn = (...args: any[]) => any;
type T = {
[k in string | symbol]: Fn;
};
Bug Report
{ [prop]() { } }[prop]
fails withType 'symbol' cannot be used as an index type.
akats(2538)
ifprop: string | symbol
. It works fine ifprop
is eitherstring
orsymbol
.🔎 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
⏯ 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.