Created
October 1, 2020 16:21
-
-
Save armanozak/098dd9e658da2618e7b78cfa5a629d4c to your computer and use it in GitHub Desktop.
[Strict Contract] How to create a binding interface #typescript #tip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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