Skip to content

Commit

Permalink
fix(o.paths): dive within arrays and deeper
Browse files Browse the repository at this point in the history
  • Loading branch information
millsp committed Mar 10, 2021
1 parent 9a678d8 commit 626beb6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions sources/List/ObjectOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {List} from './List'
* ```
*/
export type ObjectOf<O extends List> =
O extends List
O extends unknown
? number extends Length<O> // detect arrays
? _Pick<O, number> // preserves arrays
: _Omit<O, keyof any[]> // transforms tuples
: O
: never
35 changes: 15 additions & 20 deletions sources/Object/Paths.ts
Original file line number Diff line number Diff line change
@@ -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<O, Paths extends List<Key> = []> =
Length<Paths> extends 10 ? Paths : {
0: {[K in keyof O]: __Paths<O[K], Append<Paths, K>>}[keyof O]
1: {[K in keyof O]: __Paths<O[K], Append<Paths, K>>}[keyof O & number]
// It dives deep, and as it dives, it adds the paths to `Paths`
2: NonNullableFlat<OptionalFlat<Paths>>
}[
[keyof O] extends [never] ? 2 :
O extends BuiltIn | Primitive ? 2 :
O extends ReadonlyArray<any> ? 1 : 0
]
type UnionOf<A> =
A extends List
? A[number]
: A[keyof A]

/**
* @hidden
*/
export type _Paths<O extends object> =
__Paths<O> extends infer X
? Cast<X, List<Key>>
: never
type _Paths<O, P extends List = []> = UnionOf<{
[K in keyof O]:
O[K] extends BuiltIn | Primitive ? NonNullableFlat<[...P, K?]> :
[Keys<O[K]>] extends [never] ? NonNullableFlat<[...P, K?]> :
12 extends Length<P> ? NonNullableFlat<[...P, K?]> :
_Paths<O[K], [...P, K?]>
}>

/**
* Get all the possible paths of `O`
Expand All @@ -40,7 +35,7 @@ export type _Paths<O extends object> =
* ```ts
* ```
*/
export type Paths<O extends object> =
O extends unknown
? _Paths<O>
export type Paths<O, P extends List = []> =
_Paths<O, P> extends infer X
? Cast<X, List<Key>>
: never
5 changes: 4 additions & 1 deletion tests/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,11 +1053,14 @@ type O_PATHS = {
};
b: {};
};
} | {
c: boolean
};

checks([
check<O.Paths<{'prop': {a: 1}[]}>, T.NonNullable<['prop'?, number?, 'a'?]>, Test.Pass>(),
check<O.Paths<O_PATHS>, T.NonNullable<['a'?, 'a'?] | ['b'?, 'a'?, 'a'?] | ['b'?, 'b'?]>, Test.Pass>(),
check<O.Paths<O_PATHS>, T.NonNullable<['a'?, 'a'?] | ['b'?, 'a'?, 'a'?] | ['b'?, 'b'?] | ['c'?]>, Test.Pass>(),
check<O.Paths<O[]>, O.Paths<O[]>, Test.Pass>(),
])

// ---------------------------------------------------------------------------------------
Expand Down

0 comments on commit 626beb6

Please sign in to comment.