Skip to content

Instantly share code, notes, and snippets.

@ktilcu
Last active April 15, 2020 00:20
Show Gist options
  • Save ktilcu/a5c3711b24a07180f4313d5309baa574 to your computer and use it in GitHub Desktop.
Save ktilcu/a5c3711b24a07180f4313d5309baa574 to your computer and use it in GitHub Desktop.
// doesn't work K1 evaluates to never. I assume it's because `obj` isn't immediately available
// export function path<S extends object, K1 extends keyof S, K2 extends keyof S[K1], K3 extends keyof S[K1][K2]>(
// p: [K1, K2, K3]
// ): (obj: S) => S[K1][K2][K3];
// doesn't work can't index that far
// export function path<S extends object>(p: [keyof S, keyof S[keyof S], keyof S[keyof S[keyof S]]): (obj: S) => S[keyof S][keyof S[keyof S]];
export function path<S extends object>(p: [keyof S, keyof S[keyof S]]): (obj: S) => S[keyof S][keyof S[keyof S]];
export function path<S extends object>(p: [keyof S]): (obj: S) => S[keyof S];
export function path<S extends object, K1 extends keyof S>(p: [K1]): (obj: S) => S[K1];
interface IAddress {
number: string;
street: string;
zip: number;
state: string;
city: string;
}
interface IUser {
name: string;
address: IAddress;
friends: Array<IUser>;
}
const w: IUser = {
address: { number: '123', street: 'easy st', zip: 90210, state: 'AK', city: 'blah' },
friends: [],
name: 'derp'
};
const x: IUser = {
address: { number: '123', street: 'easy st', zip: 90210, state: 'AK', city: 'blah' },
friends: [w],
name: 'nope'
};
path(['friends'])(x);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment