Merge pull request #40597 from weswigham/allow-instanceof-array-to-narrow-readonly-array
Handle the mapping between Array and ReadonlyArray in isTypeDerivedFrom
This commit is contained in:
commit
84726be01a
|
@ -15509,7 +15509,7 @@ namespace ts {
|
|||
source.flags & TypeFlags.InstantiableNonPrimitive ? isTypeDerivedFrom(getBaseConstraintOfType(source) || unknownType, target) :
|
||||
target === globalObjectType ? !!(source.flags & (TypeFlags.Object | TypeFlags.NonPrimitive)) :
|
||||
target === globalFunctionType ? !!(source.flags & TypeFlags.Object) && isFunctionObjectType(source as ObjectType) :
|
||||
hasBaseType(source, getTargetType(target));
|
||||
hasBaseType(source, getTargetType(target)) || (isArrayType(target) && !isReadonlyArrayType(target) && isTypeDerivedFrom(source, globalReadonlyArrayType));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
21
tests/baselines/reference/instanceofNarrowReadonlyArray.js
Normal file
21
tests/baselines/reference/instanceofNarrowReadonlyArray.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
//// [instanceofNarrowReadonlyArray.ts]
|
||||
// @strict
|
||||
|
||||
function narrow(x: readonly number[] | number): readonly number[] {
|
||||
if (x instanceof Array) {
|
||||
return x;
|
||||
} else {
|
||||
return [x];
|
||||
}
|
||||
}
|
||||
|
||||
//// [instanceofNarrowReadonlyArray.js]
|
||||
// @strict
|
||||
function narrow(x) {
|
||||
if (x instanceof Array) {
|
||||
return x;
|
||||
}
|
||||
else {
|
||||
return [x];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
=== tests/cases/compiler/instanceofNarrowReadonlyArray.ts ===
|
||||
// @strict
|
||||
|
||||
function narrow(x: readonly number[] | number): readonly number[] {
|
||||
>narrow : Symbol(narrow, Decl(instanceofNarrowReadonlyArray.ts, 0, 0))
|
||||
>x : Symbol(x, Decl(instanceofNarrowReadonlyArray.ts, 2, 16))
|
||||
|
||||
if (x instanceof Array) {
|
||||
>x : Symbol(x, Decl(instanceofNarrowReadonlyArray.ts, 2, 16))
|
||||
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
|
||||
|
||||
return x;
|
||||
>x : Symbol(x, Decl(instanceofNarrowReadonlyArray.ts, 2, 16))
|
||||
|
||||
} else {
|
||||
return [x];
|
||||
>x : Symbol(x, Decl(instanceofNarrowReadonlyArray.ts, 2, 16))
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
=== tests/cases/compiler/instanceofNarrowReadonlyArray.ts ===
|
||||
// @strict
|
||||
|
||||
function narrow(x: readonly number[] | number): readonly number[] {
|
||||
>narrow : (x: readonly number[] | number) => readonly number[]
|
||||
>x : number | readonly number[]
|
||||
|
||||
if (x instanceof Array) {
|
||||
>x instanceof Array : boolean
|
||||
>x : number | readonly number[]
|
||||
>Array : ArrayConstructor
|
||||
|
||||
return x;
|
||||
>x : readonly number[]
|
||||
|
||||
} else {
|
||||
return [x];
|
||||
>[x] : number[]
|
||||
>x : number
|
||||
}
|
||||
}
|
9
tests/cases/compiler/instanceofNarrowReadonlyArray.ts
Normal file
9
tests/cases/compiler/instanceofNarrowReadonlyArray.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
// @strict
|
||||
|
||||
function narrow(x: readonly number[] | number): readonly number[] {
|
||||
if (x instanceof Array) {
|
||||
return x;
|
||||
} else {
|
||||
return [x];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue