Suggestion: use `type` instead of `interface`
OliverJAsh opened this issue · 1 comments
OliverJAsh commented
interface
s are open to extension, whereas type
s are not. In turn, this means that interface
s do not have index signatures (unlike type
s), which causes problems when you try to pass an interface into a function which expects an index signature. This is due to a design limitation: microsoft/TypeScript#15300.
For this reason I would recommend using type
s instead, or adding an explicit index signature to the interface
, so that the CSS module type can be passed into a function expecting an index signature.
type ObjectWithStrings = { [key: string]: string }
declare const requireObjectStrings: <T extends ObjectWithStrings>(object: T) => void;
interface MyInterface {
foo: string;
}
declare const myInterface: MyInterface;
// Error: Index signature is missing in type 'MyInterface'.
requireObjectStrings(myInterface)
type MyType = {
foo: string;
}
declare const myType: MyType;
// No error, good :-)
requireObjectStrings(myType)
OliverJAsh commented
Workaround:
type ObjectWithStrings<Key extends string> = Record<Key, string>
declare const requireObjectStrings: <Key extends string>(object: ObjectWithStrings<Key>) => void;
interface MyInterface {
foo: string;
}
declare const myInterface: MyInterface;
// No error, good :-)
requireObjectStrings(myInterface)