Skip to content

Commit

Permalink
Get: Fix handling of readonly array (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiyaaaaa authored Jan 21, 2024
1 parent 6f1db93 commit 4a38651
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions source/get.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type GetOptions = {
Like the `Get` type but receives an array of strings as a path parameter.
*/
type GetWithPath<BaseType, Keys extends readonly string[], Options extends GetOptions = {}> =
Keys extends []
Keys extends readonly []
? BaseType
: Keys extends readonly [infer Head, ...infer Tail]
? GetWithPath<
Expand Down Expand Up @@ -125,7 +125,7 @@ type PropertyOf<BaseType, Key extends string, Options extends GetOptions = {}> =
? undefined
: Key extends keyof BaseType
? StrictPropertyOf<BaseType, Key, Options>
: BaseType extends [] | [unknown, ...unknown[]]
: BaseType extends readonly [] | readonly [unknown, ...unknown[]]
? unknown // It's a tuple, but `Key` did not extend `keyof BaseType`. So the index is out of bounds.
: BaseType extends {
[n: number]: infer Item;
Expand Down
8 changes: 8 additions & 0 deletions test-d/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,11 @@ expectTypeOf<Get<WithDictionary, 'foo.whatever'>>().toEqualTypeOf<{bar: number}
expectTypeOf<Get<WithDictionary, 'foo.whatever.bar'>>().toEqualTypeOf<number | undefined>();
expectTypeOf<Get<WithDictionary, 'baz.whatever.qux[3].x'>>().toEqualTypeOf<boolean | undefined>();
expectTypeOf<Get<WithDictionary, ['baz', 'whatever', 'qux', '3', 'x']>>().toEqualTypeOf<boolean | undefined>();

// Test array index out of bounds
expectTypeOf<Get<{a: []}, 'a[0]'>>().toEqualTypeOf<unknown>();
expectTypeOf<Get<{a: readonly []}, 'a[0]'>>().toEqualTypeOf<unknown>();

// Test empty path array
expectTypeOf<WithDictionary>().toEqualTypeOf<Get<WithDictionary, []>>();
expectTypeOf<WithDictionary>().toEqualTypeOf<Get<WithDictionary, readonly []>>();

0 comments on commit 4a38651

Please sign in to comment.