Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: use type instead of interface #11

Open
OliverJAsh opened this issue Apr 17, 2019 · 1 comment
Open

Suggestion: use type instead of interface #11

OliverJAsh opened this issue Apr 17, 2019 · 1 comment

Comments

@OliverJAsh
Copy link

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)
@OliverJAsh
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant