Skip to content

Commit 84726be

Browse files
authored
Merge pull request microsoft#40597 from weswigham/allow-instanceof-array-to-narrow-readonly-array
Handle the mapping between Array and ReadonlyArray in isTypeDerivedFrom
2 parents 8850e0d + 081f982 commit 84726be

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15509,7 +15509,7 @@ namespace ts {
1550915509
source.flags & TypeFlags.InstantiableNonPrimitive ? isTypeDerivedFrom(getBaseConstraintOfType(source) || unknownType, target) :
1551015510
target === globalObjectType ? !!(source.flags & (TypeFlags.Object | TypeFlags.NonPrimitive)) :
1551115511
target === globalFunctionType ? !!(source.flags & TypeFlags.Object) && isFunctionObjectType(source as ObjectType) :
15512-
hasBaseType(source, getTargetType(target));
15512+
hasBaseType(source, getTargetType(target)) || (isArrayType(target) && !isReadonlyArrayType(target) && isTypeDerivedFrom(source, globalReadonlyArrayType));
1551315513
}
1551415514

1551515515
/**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [instanceofNarrowReadonlyArray.ts]
2+
// @strict
3+
4+
function narrow(x: readonly number[] | number): readonly number[] {
5+
if (x instanceof Array) {
6+
return x;
7+
} else {
8+
return [x];
9+
}
10+
}
11+
12+
//// [instanceofNarrowReadonlyArray.js]
13+
// @strict
14+
function narrow(x) {
15+
if (x instanceof Array) {
16+
return x;
17+
}
18+
else {
19+
return [x];
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/instanceofNarrowReadonlyArray.ts ===
2+
// @strict
3+
4+
function narrow(x: readonly number[] | number): readonly number[] {
5+
>narrow : Symbol(narrow, Decl(instanceofNarrowReadonlyArray.ts, 0, 0))
6+
>x : Symbol(x, Decl(instanceofNarrowReadonlyArray.ts, 2, 16))
7+
8+
if (x instanceof Array) {
9+
>x : Symbol(x, Decl(instanceofNarrowReadonlyArray.ts, 2, 16))
10+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
11+
12+
return x;
13+
>x : Symbol(x, Decl(instanceofNarrowReadonlyArray.ts, 2, 16))
14+
15+
} else {
16+
return [x];
17+
>x : Symbol(x, Decl(instanceofNarrowReadonlyArray.ts, 2, 16))
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/instanceofNarrowReadonlyArray.ts ===
2+
// @strict
3+
4+
function narrow(x: readonly number[] | number): readonly number[] {
5+
>narrow : (x: readonly number[] | number) => readonly number[]
6+
>x : number | readonly number[]
7+
8+
if (x instanceof Array) {
9+
>x instanceof Array : boolean
10+
>x : number | readonly number[]
11+
>Array : ArrayConstructor
12+
13+
return x;
14+
>x : readonly number[]
15+
16+
} else {
17+
return [x];
18+
>[x] : number[]
19+
>x : number
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @strict
2+
3+
function narrow(x: readonly number[] | number): readonly number[] {
4+
if (x instanceof Array) {
5+
return x;
6+
} else {
7+
return [x];
8+
}
9+
}

0 commit comments

Comments
 (0)