From 74341a01d72a10024eaa62f9343d9abc36dcb1aa Mon Sep 17 00:00:00 2001 From: Orta Date: Tue, 12 Jan 2021 19:24:10 +0000 Subject: [PATCH] A second attempt at readonly array support persisting through isArray --- lib/lib.es5.d.ts | 2 +- src/compiler/checker.ts | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index 221882e80f6d5..ab6a5d0fb3806 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -1408,7 +1408,7 @@ interface ArrayConstructor { (arrayLength?: number): any[]; (arrayLength: number): T[]; (...items: T[]): T[]; - isArray(arg: any): arg is any[]; + isArray(arg: any): arg is readonly unknown[]; readonly prototype: any[]; } diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 10f8dc923b88f..fab3f26ea4d40 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22999,11 +22999,16 @@ namespace ts { } } - // If the candidate type is a subtype of the target type, narrow to the candidate type, - // if the target type is a subtype of the candidate type, narrow to the target type, - // otherwise, narrow to an intersection of the two types. - return isTypeSubtypeOf(candidate, type) ? candidate : isTypeSubtypeOf(type, candidate) ? type : getIntersectionType([type, candidate]); - } + // If the candidate type is a subtype of the target type, narrow to the candidate type. + // Otherwise, if the target type is assignable to the candidate type, keep the target type. + // Otherwise, if the candidate type is assignable to the target type, narrow to the candidate + // type. Otherwise, the types are completely unrelated, so narrow to an intersection of the + // two types. + return isTypeSubtypeOf(candidate, type) ? candidate : + isTypeAssignableTo(type, candidate) ? type : + isTypeAssignableTo(candidate, type) ? candidate : + getIntersectionType([type, candidate]); + } function narrowTypeByCallExpression(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type { if (hasMatchingArgument(callExpression, reference)) {