Skip to content

Commit 081f982

Browse files
committed
Handle the mapping between Array and ReadonlyArray in isTypeDerivedFrom
1 parent a5babe1 commit 081f982

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
@@ -15349,7 +15349,7 @@ namespace ts {
1534915349
source.flags & TypeFlags.InstantiableNonPrimitive ? isTypeDerivedFrom(getBaseConstraintOfType(source) || unknownType, target) :
1535015350
target === globalObjectType ? !!(source.flags & (TypeFlags.Object | TypeFlags.NonPrimitive)) :
1535115351
target === globalFunctionType ? !!(source.flags & TypeFlags.Object) && isFunctionObjectType(source as ObjectType) :
15352-
hasBaseType(source, getTargetType(target));
15352+
hasBaseType(source, getTargetType(target)) || (isArrayType(target) && !isReadonlyArrayType(target) && isTypeDerivedFrom(source, globalReadonlyArrayType));
1535315353
}
1535415354

1535515355
/**
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)