From 626beb61b3100c5e4fd699c946e0e2fed7e24cb6 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Mills Date: Wed, 10 Mar 2021 16:41:22 +0100 Subject: [PATCH] fix(o.paths): dive within arrays and deeper --- sources/List/ObjectOf.ts | 4 ++-- sources/Object/Paths.ts | 35 +++++++++++++++-------------------- tests/Object.ts | 5 ++++- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/sources/List/ObjectOf.ts b/sources/List/ObjectOf.ts index 42b679b41..1fbb46850 100644 --- a/sources/List/ObjectOf.ts +++ b/sources/List/ObjectOf.ts @@ -12,8 +12,8 @@ import {List} from './List' * ``` */ export type ObjectOf = - O extends List + O extends unknown ? number extends Length // detect arrays ? _Pick // preserves arrays : _Omit // transforms tuples - : O + : never diff --git a/sources/Object/Paths.ts b/sources/Object/Paths.ts index 288fa9550..878ff0056 100644 --- a/sources/Object/Paths.ts +++ b/sources/Object/Paths.ts @@ -1,35 +1,30 @@ -import {OptionalFlat} from '../Object/Optional' import {Key} from '../Any/Key' import {NonNullableFlat} from '../Object/NonNullable' import {Cast} from '../Any/Cast' import {List} from '../List/List' -import {Append} from '../List/Append' import {BuiltIn} from '../Misc/BuiltIn' import {Primitive} from '../Misc/Primitive' import {Length} from '../List/Length' +import {Keys} from '../Any/Keys' /** * @hidden */ -type __Paths = []> = -Length extends 10 ? Paths : { - 0: {[K in keyof O]: __Paths>}[keyof O] - 1: {[K in keyof O]: __Paths>}[keyof O & number] - // It dives deep, and as it dives, it adds the paths to `Paths` - 2: NonNullableFlat> -}[ - [keyof O] extends [never] ? 2 : - O extends BuiltIn | Primitive ? 2 : - O extends ReadonlyArray ? 1 : 0 -] +type UnionOf = + A extends List + ? A[number] + : A[keyof A] /** * @hidden */ -export type _Paths = - __Paths extends infer X - ? Cast> - : never +type _Paths = UnionOf<{ + [K in keyof O]: + O[K] extends BuiltIn | Primitive ? NonNullableFlat<[...P, K?]> : + [Keys] extends [never] ? NonNullableFlat<[...P, K?]> : + 12 extends Length

? NonNullableFlat<[...P, K?]> : + _Paths +}> /** * Get all the possible paths of `O` @@ -40,7 +35,7 @@ export type _Paths = * ```ts * ``` */ -export type Paths = - O extends unknown - ? _Paths +export type Paths = + _Paths extends infer X + ? Cast> : never diff --git a/tests/Object.ts b/tests/Object.ts index bea40e320..b6ccfb165 100644 --- a/tests/Object.ts +++ b/tests/Object.ts @@ -1053,11 +1053,14 @@ type O_PATHS = { }; b: {}; }; +} | { + c: boolean }; checks([ check, T.NonNullable<['prop'?, number?, 'a'?]>, Test.Pass>(), - check, T.NonNullable<['a'?, 'a'?] | ['b'?, 'a'?, 'a'?] | ['b'?, 'b'?]>, Test.Pass>(), + check, T.NonNullable<['a'?, 'a'?] | ['b'?, 'a'?, 'a'?] | ['b'?, 'b'?] | ['c'?]>, Test.Pass>(), + check, O.Paths, Test.Pass>(), ]) // ---------------------------------------------------------------------------------------