From 1696a2f5793f147099cb2784507bc0836efcb1bd Mon Sep 17 00:00:00 2001 From: imperosol Date: Sat, 14 Dec 2024 00:06:18 +0100 Subject: [PATCH] Add NestedKeyOf Type --- core/static/bundled/utils/types.d.ts | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 core/static/bundled/utils/types.d.ts diff --git a/core/static/bundled/utils/types.d.ts b/core/static/bundled/utils/types.d.ts new file mode 100644 index 00000000..e9040c67 --- /dev/null +++ b/core/static/bundled/utils/types.d.ts @@ -0,0 +1,37 @@ +/** + * A key of an object, or of one of its descendants. + * + * Example : + * ```typescript + * interface Foo { + * foo_inner: number; + * } + * + * interface Bar { + * foo: Foo; + * } + * + * const foo = (key: NestedKeyOf) { + * console.log(key); + * } + * + * foo("foo.foo_inner"); // OK + * foo("foo.bar"); // FAIL + * ``` + */ +export type NestedKeyOf = { + [Key in keyof T & (string | number)]: NestedKeyOfHandleValue; +}[keyof T & (string | number)]; + +type NestedKeyOfInner = { + [Key in keyof T & (string | number)]: NestedKeyOfHandleValue< + T[Key], + `['${Key}']` | `.${Key}` + >; +}[keyof T & (string | number)]; + +type NestedKeyOfHandleValue = T extends unknown[] + ? Text + : T extends object + ? Text | `${Text}${NestedKeyOfInner}` + : Text;