From 8120094c81236651da967c33d7bd5ca185188faf Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 22 May 2019 06:49:49 -0700 Subject: [PATCH] Simplify index and object types when obtaining indexed access constraint --- src/compiler/checker.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e9dddff64af39..bd8d4c05901c7 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7657,15 +7657,20 @@ namespace ts { return hasNonCircularBaseConstraint(type) ? getConstraintFromIndexedAccess(type) : undefined; } + function getSimplifiedTypeOrConstraint(type: Type) { + const simplified = getSimplifiedType(type, /*writing*/ false); + return simplified !== type ? simplified : getConstraintOfType(type); + } + function getConstraintFromIndexedAccess(type: IndexedAccessType) { - const indexConstraint = getConstraintOfType(type.indexType); + const indexConstraint = getSimplifiedTypeOrConstraint(type.indexType); if (indexConstraint && indexConstraint !== type.indexType) { const indexedAccess = getIndexedAccessTypeOrUndefined(type.objectType, indexConstraint); if (indexedAccess) { return indexedAccess; } } - const objectConstraint = getConstraintOfType(type.objectType); + const objectConstraint = getSimplifiedTypeOrConstraint(type.objectType); if (objectConstraint && objectConstraint !== type.objectType) { return getIndexedAccessTypeOrUndefined(objectConstraint, type.indexType); }