Skip to content

Commit ecaa87d

Browse files
daishugeclaude
andcommitted
fix(lib): preserve readonly narrowing in Array.isArray type guard
Adds an overload to ArrayConstructor.isArray that narrows readonly array types correctly, fixing a 9-year-old issue where Array.isArray() would lose readonly type information. Fixes #17002 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 637d574 commit ecaa87d

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/lib/es5.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,7 @@ interface ArrayConstructor {
14991499
(arrayLength?: number): any[];
15001500
<T>(arrayLength: number): T[];
15011501
<T>(...items: T[]): T[];
1502+
isArray(arg: readonly any[] | any): arg is readonly any[];
15021503
isArray(arg: any): arg is any[];
15031504
readonly prototype: any[];
15041505
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @strict: true
2+
3+
function test1(x: readonly string[] | string) {
4+
if (Array.isArray(x)) {
5+
x; // should be readonly string[]
6+
}
7+
}
8+
9+
function test2(x: readonly number[] | number) {
10+
if (Array.isArray(x)) {
11+
x; // should be readonly number[]
12+
x[0]; // should be number
13+
}
14+
}
15+
16+
function test3(x: string[] | string) {
17+
if (Array.isArray(x)) {
18+
x; // should still be string[] (not readonly)
19+
}
20+
}
21+
22+
function test4(x: unknown) {
23+
if (Array.isArray(x)) {
24+
x; // should still be any[] (existing behavior)
25+
}
26+
}

0 commit comments

Comments
 (0)