From 7233cde0dc61156a98b8d290449a183e986dc22b Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 24 Jul 2018 14:49:22 -0700 Subject: [PATCH] Simplify logic in getBaseConstraint --- src/compiler/checker.ts | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c56d39a9ffc..c1366745c39 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4370,8 +4370,7 @@ namespace ts { case TypeSystemPropertyName.ResolvedReturnType: return !!(target).resolvedReturnType; case TypeSystemPropertyName.ImmediateBaseConstraint: - const bc = (target).immediateBaseConstraint; - return !!bc && bc !== circularConstraintType; + return !!(target).immediateBaseConstraint; } return Debug.fail("Unhandled TypeSystemPropertyName " + propertyName); } @@ -6987,30 +6986,26 @@ namespace ts { * circularly references the type variable. */ function getResolvedBaseConstraint(type: InstantiableType | UnionOrIntersectionType): Type { - let circular: boolean | undefined; - if (!type.resolvedBaseConstraint) { - const constraint = getBaseConstraint(type); - type.resolvedBaseConstraint = circular ? circularConstraintType : getTypeWithThisArgument(constraint || noConstraintType, type); + return type.resolvedBaseConstraint || + (type.resolvedBaseConstraint = getTypeWithThisArgument(getImmediateBaseConstraint(type), type)); + + function getImmediateBaseConstraint(t: Type): Type { + if (!t.immediateBaseConstraint) { + if (!pushTypeResolution(t, TypeSystemPropertyName.ImmediateBaseConstraint)) { + return circularConstraintType; + } + let result = computeBaseConstraint(getSimplifiedType(t)); + if (!popTypeResolution()) { + result = circularConstraintType; + } + t.immediateBaseConstraint = result || noConstraintType; + } + return t.immediateBaseConstraint; } - return type.resolvedBaseConstraint; function getBaseConstraint(t: Type): Type | undefined { - if (t.immediateBaseConstraint) { - return t.immediateBaseConstraint === noConstraintType ? undefined : t.immediateBaseConstraint; - } - if (!pushTypeResolution(t, TypeSystemPropertyName.ImmediateBaseConstraint)) { - circular = true; - t.immediateBaseConstraint = circularConstraintType; - return undefined; - } - const result = computeBaseConstraint(getSimplifiedType(t)); - if (!popTypeResolution()) { - circular = true; - t.immediateBaseConstraint = circularConstraintType; - return undefined; - } - t.immediateBaseConstraint = !result ? noConstraintType : result; - return result; + const c = getImmediateBaseConstraint(t); + return c !== noConstraintType && c !== circularConstraintType ? c : undefined; } function computeBaseConstraint(t: Type): Type | undefined {