Skip to content

Commit 7c82a21

Browse files
authored
Paths: Fix behavior with tuples containing optional elements with a rest element (#1346)
1 parent f6cf954 commit 7c82a21

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

source/paths.d.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {StaticPartOfArray, VariablePartOfArray, NonRecursiveType, ToString, IsNumberLike, ApplyDefaultOptions} from './internal/index.d.ts';
1+
import type {NonRecursiveType, ToString, IsNumberLike, ApplyDefaultOptions} from './internal/index.d.ts';
22
import type {IsAny} from './is-any.d.ts';
33
import type {UnknownArray} from './unknown-array.d.ts';
44
import type {GreaterThan} from './greater-than.d.ts';
@@ -196,14 +196,9 @@ type _Paths<T, Options extends Required<PathsOptions>, CurrentDepth extends numb
196196
? never
197197
: IsAny<T> extends true
198198
? never
199-
: T extends UnknownArray
200-
? number extends T['length']
201-
// We need to handle the fixed and non-fixed index part of the array separately.
202-
? InternalPaths<StaticPartOfArray<T>, Options, CurrentDepth> | InternalPaths<Array<VariablePartOfArray<T>[number]>, Options, CurrentDepth>
203-
: InternalPaths<T, Options, CurrentDepth>
204-
: T extends object
205-
? InternalPaths<T, Options, CurrentDepth>
206-
: never;
199+
: T extends object
200+
? InternalPaths<T, Options, CurrentDepth>
201+
: never;
207202

208203
type InternalPaths<T, Options extends Required<PathsOptions>, CurrentDepth extends number> =
209204
Options['maxRecursionDepth'] extends infer MaxDepth extends number

test-d/paths.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ expectType<number | `${number}` | '0.a' | `${number}.b`>(trailingSpreadTuple);
9292
declare const trailingSpreadTuple1: Paths<[{a: string}, {b: number}, ...Array<{c: number}>]>;
9393
expectType<number | `${number}` | '0.a' | '1.b' | `${number}.c`>(trailingSpreadTuple1);
9494

95+
declare const optionalElementsWithTrailingSpreadTuple: Paths<{foo: [{a: string}, ({b: number})?, ...Array<{c: number}>]}>;
96+
expectType<'foo' | `foo.${number}` | 'foo.0.a' | 'foo.1.b' | `foo.${number}.c`>(optionalElementsWithTrailingSpreadTuple);
97+
98+
declare const optionalElementsWithTrailingSpreadTuple1: Paths<[({a: string})?, ({b: number})?, ...Array<{c: number}>]>;
99+
expectType<number | `${number}` | '0.a' | '1.b' | `${number}.c`>(optionalElementsWithTrailingSpreadTuple1);
100+
95101
declare const leadingSpreadTuple: Paths<[...Array<{a: string}>, {b: number}]>;
96102
expectType<number | `${number}` | `${number}.b` | `${number}.a`>(leadingSpreadTuple);
97103

0 commit comments

Comments
 (0)