seek-oss/css-modules-typescript-loader

Suggestion: use `type` instead of `interface`

OliverJAsh opened this issue · 1 comments

interfaces are open to extension, whereas types are not. In turn, this means that interfaces do not have index signatures (unlike types), 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 types 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)

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)