Skip to content

Instantly share code, notes, and snippets.

@hediet
Created April 21, 2017 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hediet/d0c20ae606d5da2fed9a742e731c06aa to your computer and use it in GitHub Desktop.
Save hediet/d0c20ae606d5da2fed9a742e731c06aa to your computer and use it in GitHub Desktop.
Acceptor / User Pattern for TypeScript
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