Created
April 21, 2017 19:29
-
-
Save hediet/d0c20ae606d5da2fed9a742e731c06aa to your computer and use it in GitHub Desktop.
Acceptor / User Pattern for TypeScript
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
interface Acceptor<T extends string> { | |
accept(x: UserOf<T>): any; | |
allow<T1 extends string>(x: T1): Acceptor<T|T1>; | |
} | |
interface UserOf<T extends string> { | |
_brand: T; | |
} | |
function use<T extends string>(name: T): UserOf<T> { return null!; } | |
function combine<T1 extends string, T2 extends string>(x: UserOf<T1>, y: UserOf<T2>): UserOf<T1|T2> { return null!; } | |
const a = use("a"); | |
const b = use("b"); | |
const ab = combine(a, b); | |
let f: Acceptor<never> = null!; | |
f.allow("a").allow("b").accept(ab); | |
f.allow("a").accept(ab); | |
f.allow("b").accept(ab); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment