Created
April 7, 2022 11:46
-
-
Save vendethiel/10e579c63ee23a7b373ef4dffeb0180a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.typescriptlang.org/play?#code/PTAEDEEsDsFMCgDGB7aBnALqA1rAngFygDkAgsaALwnkDcSqmoADgIYBOas7RAFAJRUAfKGgBXALYAjblVADhoAIz0U6LADNkyIgCVYKdgBMAPBjzNYyDTnwAaUOcvWWHLuxHUA3gG1ceAF0iNk5uAF96eBBQADlkTRgEDTFoRAxIVFAta2gTAGlQWAAPDFhoIzRQTHYYAHMHADUhXn8iPIcQ9z5BShEG-j0DZGN8hwVe0CbQL3hQOdA1Jh1QfUNTdvkevs9pv3wg11D2CNn59lgMMXZoUGR6MPggA