Skip to content

Instantly share code, notes, and snippets.

@armanozak
Created October 1, 2020 16:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armanozak/098dd9e658da2618e7b78cfa5a629d4c to your computer and use it in GitHub Desktop.
Save armanozak/098dd9e658da2618e7b78cfa5a629d4c to your computer and use it in GitHub Desktop.
[Strict Contract] How to create a binding interface #typescript #tip
type Strict<Contract, Class> = Class extends Contract
? { [K in keyof Class]: K extends keyof Contract ? Contract[K] : never }
: Contract;
interface MyContract {
foo: number;
bar: boolean;
}
type MyStrictContract = Strict<MyContract, MyClass>;
class MyClass implements MyStrictContract {
private _qux = '';
foo = 0;
bar = false;
baz() {} // error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment