Skip to content

Commit

Permalink
Paths: Ensure it doesn't recurse into Map or Set's prototype pr…
Browse files Browse the repository at this point in the history
…operties (#772)
  • Loading branch information
Emiyaaaaa authored Nov 28, 2023
1 parent 7144aed commit 6759853
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/paths.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ open('listB.1'); // TypeError. Because listB only has one element.
@category Array
*/
export type Paths<T> =
T extends NonRecursiveType
T extends NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
? never
: IsAny<T> extends true
? never
Expand Down
18 changes: 18 additions & 0 deletions test-d/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ expectType<'a'>(record);
declare const record2: Paths<Record<1, unknown>>;
expectType<1 | '1'>(record2);

declare const map: Paths<{foo?: {bar?: Map<string, number>}}>;
expectType<'foo' | 'foo.bar'>(map);

declare const map2: Paths<Map<string, number>>;
expectType<never>(map2);

declare const readonlyMap: Paths<{foo?: {bar?: ReadonlyMap<string, number>}}>;
expectType<'foo' | 'foo.bar'>(readonlyMap);

declare const set: Paths<{foo?: {bar?: Set<string>}}>;
expectType<'foo' | 'foo.bar'>(set);

declare const set2: Paths<Set<string>>;
expectType<never>(set2);

declare const readonlySet: Paths<{foo?: {bar?: ReadonlySet<string>}}>;
expectType<'foo' | 'foo.bar'>(readonlySet);

// Test for unknown length array
declare const trailingSpreadTuple: Paths<[{a: string}, ...Array<{b: number}>]>;
expectType<number | `${number}` | '0.a' | `${number}.b`>(trailingSpreadTuple);
Expand Down

0 comments on commit 6759853

Please sign in to comment.