Last active
May 28, 2018 13:58
-
-
Save susisu/8378b9edb94edb1e2019909f2c95e7f9 to your computer and use it in GitHub Desktop.
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 IParser<T> { } | |
declare class Opt<K extends string, T> { | |
constructor(key: K, parser: IParser<T>); | |
} | |
declare const string: IParser<string>; | |
declare const number: IParser<number>; | |
declare class OptParser<P extends {}> { | |
add<K extends string, T>( | |
opt: Opt<K, T> | |
): K extends keyof P ? {} : OptParser<{ [key in K]: T } & P>; | |
run(callback: (obj: P) => void): void; | |
} | |
declare const empty: OptParser<{}>; | |
const p1 = empty | |
.add(new Opt("foo", string)) | |
.add(new Opt("bar", number)); | |
p1.run((opt) => { | |
opt.foo; | |
opt.bar; | |
opt.baz; // error: no option for "baz" | |
}); | |
const p2 = empty | |
.add(new Opt("foo", string)) | |
.add(new Opt("bar", number)) | |
.add(new Opt("foo", number)) // conflict (or duplicate) | |
.add(new Opt("baz", number)); // error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment