Skip to content

Instantly share code, notes, and snippets.

@jRimbault jRimbault/dot.ts
Last active Dec 28, 2019

Embed
What would you like to do?
export type DeepDotKey<T> = {
[P in keyof T]: DeepDotKey<T[P]>;
} & (() => string);
/**
* Type safe string builder deep dot notation
*
* @example
* interface Foo {
* dictionary: { [key: string]: { value: number } | undefined };
* field: { prop: number };
* list: { node: string }[];
* }
*
* const _ = dot<Foo>();
* const key1 = _.field.prop(); // key1 === 'field.prop'
* const key2 = _.dictionary['hello'].value(); // key2 === 'dictionary.hello.value'
* const key3 = _.list[3].node(); // key3 === 'list.3.node'
*/
export function dot<T>(prev?: string): DeepDotKey<T> {
return new Proxy(() => prev, {
get: (_, next: string) => dot(prev ? `${prev}.${next}` : next),
}) as DeepDotKey<T>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.