Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Created June 9, 2020 21:34
Show Gist options
  • Save VitorLuizC/90504dd4b82986f78a2af3f6aa0da6ae to your computer and use it in GitHub Desktop.
Save VitorLuizC/90504dd4b82986f78a2af3f6aa0da6ae to your computer and use it in GitHub Desktop.
type JsonObject = { [property: string]: Json };
type JsonArray = Json[];
type Json = null | boolean | number | string | JsonObject | JsonArray;
type NodeOf<T extends Json> = T extends JsonObject
? T
: T extends JsonArray
? T[number]
: never;
type X = {
property1: string;
property2: null;
property3: string;
property4: boolean;
property5: boolean;
property6: number;
property7: {
name: string;
};
};
type XNode = NodeOf<X[keyof X]>;
//=> { name: string; }
type Y = {
property1: number;
property2: null;
property3: string;
property4: boolean;
property5: boolean;
property6: number[];
property7: {
value: number;
}[];
};
type YNode = NodeOf<Y[keyof Y]>;
//=> { value: number }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment