I want to make a generic class that requires a template parameter that is an interface with only string keys.
I thought I could do something like
class MyClass<T extends Record<string, object>> {
sendEventData<TKey extends keyof T>(event: TKey, data: T[TKey]) {
// ...
}
}
However, if I instantiate it like
interface MyEvents {
someEvent: { foo: string }
}
const instanace = new MyClass<MyEvents>();
I get a compilation error:
Type 'MyEvents' does not satisfy the constraint 'Record<string, object>'.
Index signature is missing in type 'MyEvents'.
If I remove extends Record<string, object entirely, it compiles fine, but it doesn't restrict it to a map of string => object.