Skip to content

Commit

Permalink
Fix instantiation error for any in PathKeys type #929
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Nov 17, 2024
1 parent c38491d commit a2b85b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
1 change: 1 addition & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ All notable changes to the library will be documented in this file.
- Change type signature of `findItem` action to support type predicates (issue #867)
- Refactor `bytes`, `maxBytes`, `minBytes` and `notBytes` action
- Fix implementation of `nonOptional`, `nonOptionalAsync`, `nonNullable`, `nonNullableAsync`, `nonNullish` and `nonNullishAsync` schema in edge cases (issue #909)
- Fix instantiation error for `any` in `PathKeys` type (issue #929)

## v0.42.1 (September 20, 2024)

Expand Down
33 changes: 20 additions & 13 deletions library/src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,30 @@ export type UnionToTuple<TUnion> =
? [...UnionToTuple<Exclude<TUnion, TLast>>, TLast]
: [];

/**
* Checks if a type is `any`.
*/
type IsAny<Type> = 0 extends 1 & Type ? true : false;

/**
* Extracts tuples with path keys.
*/
export type PathKeys<TValue> = MaybeReadonly<
TValue extends readonly unknown[]
? number extends TValue['length']
? [number] | [number, ...PathKeys<TValue[number]>]
: {
[TKey in keyof TValue]: TKey extends `${infer TIndex extends number}`
? [TIndex] | [TIndex, ...PathKeys<TValue[TKey]>]
: never;
}[keyof TValue & number]
: TValue extends Record<string, unknown>
? {
[TKey in keyof TValue]: [TKey] | [TKey, ...PathKeys<TValue[TKey]>];
}[keyof TValue]
: never
IsAny<TValue> extends true
? never
: TValue extends readonly unknown[]
? number extends TValue['length']
? [number] | [number, ...PathKeys<TValue[number]>]
: {
[TKey in keyof TValue]: TKey extends `${infer TIndex extends number}`
? [TIndex] | [TIndex, ...PathKeys<TValue[TKey]>]
: never;
}[keyof TValue & number]
: TValue extends Record<string, unknown>
? {
[TKey in keyof TValue]: [TKey] | [TKey, ...PathKeys<TValue[TKey]>];
}[keyof TValue]
: never
>;

/**
Expand Down

0 comments on commit a2b85b4

Please sign in to comment.