mirror of
https://github.com/ae-utbm/sith.git
synced 2024-12-23 00:01:16 +00:00
Add NestedKeyOf Type
This commit is contained in:
parent
baebc0b690
commit
1696a2f579
37
core/static/bundled/utils/types.d.ts
vendored
Normal file
37
core/static/bundled/utils/types.d.ts
vendored
Normal file
@ -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<Bar>) {
|
||||
* console.log(key);
|
||||
* }
|
||||
*
|
||||
* foo("foo.foo_inner"); // OK
|
||||
* foo("foo.bar"); // FAIL
|
||||
* ```
|
||||
*/
|
||||
export type NestedKeyOf<T extends object> = {
|
||||
[Key in keyof T & (string | number)]: NestedKeyOfHandleValue<T[Key], `${Key}`>;
|
||||
}[keyof T & (string | number)];
|
||||
|
||||
type NestedKeyOfInner<T extends object> = {
|
||||
[Key in keyof T & (string | number)]: NestedKeyOfHandleValue<
|
||||
T[Key],
|
||||
`['${Key}']` | `.${Key}`
|
||||
>;
|
||||
}[keyof T & (string | number)];
|
||||
|
||||
type NestedKeyOfHandleValue<T, Text extends string> = T extends unknown[]
|
||||
? Text
|
||||
: T extends object
|
||||
? Text | `${Text}${NestedKeyOfInner<T>}`
|
||||
: Text;
|
Loading…
Reference in New Issue
Block a user