Skip to content

Instantly share code, notes, and snippets.

@ChristianGaertner
Last active August 23, 2019 09:49
Show Gist options
  • Save ChristianGaertner/e6e5c67a18f5e174e4114e73fc50ac38 to your computer and use it in GitHub Desktop.
Save ChristianGaertner/e6e5c67a18f5e174e4114e73fc50ac38 to your computer and use it in GitHub Desktop.
enum Gender {
MALE,
FEMALE,
UNKNOWN,
}
type Person = {
name: string;
age: number;
verified: boolean;
gender: Gender;
children: Person[];
useless: null;
someTuple: [string, number];
};
type FormModel = {
entries: Person[];
main: Person;
}
type TypedPathGenerator = {
getPath(): string;
}
type TypedArrayWrapper<E, T extends E[]> = TypedPathGenerator & {
[index: number]: TypedPathWrapper<E>;
}
type Primitives = string | number | boolean | null | undefined;
type TypedValueWrapper<T extends Primitives> = TypedPathGenerator;
type TypedPathWrapper<T extends Object> = TypedPathGenerator & {
[K in keyof T]: T[K] extends (infer E)[] ? TypedArrayWrapper<E, T[K]> : T[K] extends Primitives ? TypedValueWrapper<T[K]> : TypedPathWrapper<T[K]>;
};
type FormXYPath = TypedPathWrapper<FormModel>;
function foo(x: FormXYPath) {
x.entries[0].age.getPath();
x.main.gender.getPath();
x.main.children[1].gender.getPath();
x.main.gender.getPath();
x.main.verified.getPath();
x.main.useless.getPath();
x.main.someTuple.getPath();
x.main.getPath();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment