I'm trying to create a type to describe an ES6 Proxy object where I will know the types for a few keys, and the rest of the keys will be generic with a callback as a value and I won't know their names until runtime.
However, if I try something like this:
interface MyCallback {
(): void;
}
interface MyType {
myKey1: number;
[key: string]: MyCallBack;
}
I get errors like:
[ts] Property 'myKey1' of type 'number' is not assignable to string index type 'MyCallback'.
If I add [key: string]: number
, I get the error Duplicate string index signature
.
If I overload it so it's like number | MyCallback
, I get this error if I try to call a callback on a MyType
instance:
[ts] Cannot invoke an expression whose type lacks a call signature. Type 'number | MyCallback' has no compatible call signatures.
Is it possible to have a type like I'm trying to create in TypeScript?