Skip to content

Instantly share code, notes, and snippets.

@vendethiel
Created April 7, 2022 11:46
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 vendethiel/10e579c63ee23a7b373ef4dffeb0180a to your computer and use it in GitHub Desktop.
Save vendethiel/10e579c63ee23a7b373ef4dffeb0180a to your computer and use it in GitHub Desktop.
type WithKey<T extends object, K extends string> = Record<K, string> & T;
class ParseObject<T extends object> {
public constructor(private parts: (keyof T)[]) {
}
public add<K extends string>(k: K): ParseObject<WithKey<T, K>> {
const parts: (keyof WithKey<T, K>)[] = [...this.parts, k];
return new ParseObject<WithKey<T, K>>(parts);
}
static build(): ParseObject<{}> {
return new ParseObject([]);
}
public reify(): T {
const foo: [keyof T, string][] = this.parts.map((p) => [p, "foo"]);
const o: T = Object.fromEntries(foo);
return o;
}
}
function parse2<T extends string>(xs: T[]): Record<T, string> {
throw 1;
}
const a: ParseObject<{}> = ParseObject.build();
const b: ParseObject<{foo: string}> = a.add('foo');
const c: ParseObject<{foo: string, bar: string}> = b.add('foo');