From 3d3ed04b2868f5b74a6326f1eec8988e98309aca Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 25 Aug 2017 08:49:34 -0700 Subject: [PATCH] Perform indexed access type transformations consistently --- src/compiler/checker.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 338af6b3ba3..4d08292a9f2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5906,6 +5906,10 @@ namespace ts { } function getConstraintOfIndexedAccess(type: IndexedAccessType) { + const transformed = getTransformedIndexedAccessType(type); + if (transformed) { + return transformed; + } const baseObjectType = getBaseConstraintOfType(type.objectType); const baseIndexType = getBaseConstraintOfType(type.indexType); return baseObjectType || baseIndexType ? getIndexedAccessType(baseObjectType || type.objectType, baseIndexType || type.indexType) : undefined; @@ -7617,8 +7621,8 @@ namespace ts { return false; } - // Transform an indexed access occurring in a read position to a simpler form. Return the simpler form, - // or undefined if no transformation is possible. + // Transform an indexed access to a simpler form, if possible. Return the simpler form, or return + // undefined if no transformation is possible. function getTransformedIndexedAccessType(type: IndexedAccessType): Type { const objectType = type.objectType; // Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or @@ -9279,7 +9283,7 @@ namespace ts { else if (target.flags & TypeFlags.IndexedAccess) { // A type S is related to a type T[K] if S is related to A[K], where K is string-like and // A is the apparent type of S. - const constraint = getConstraintOfType(target); + const constraint = getConstraintOfIndexedAccess(target); if (constraint) { if (result = isRelatedTo(source, constraint, reportErrors)) { errorInfo = saveErrorInfo; @@ -9319,7 +9323,7 @@ namespace ts { else if (source.flags & TypeFlags.IndexedAccess) { // A type S[K] is related to a type T if A[K] is related to T, where K is string-like and // A is the apparent type of S. - const constraint = getTransformedIndexedAccessType(source) || getConstraintOfIndexedAccess(source); + const constraint = getConstraintOfIndexedAccess(source); if (constraint) { if (result = isRelatedTo(constraint, target, reportErrors)) { errorInfo = saveErrorInfo;