Skip to content

Instantly share code, notes, and snippets.

@susisu
Last active May 28, 2018 13:58
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 susisu/8378b9edb94edb1e2019909f2c95e7f9 to your computer and use it in GitHub Desktop.
Save susisu/8378b9edb94edb1e2019909f2c95e7f9 to your computer and use it in GitHub Desktop.
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