Skip to content

Commit

Permalink
Conditionally clone type param
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed May 23, 2019
1 parent 83b226a commit 9133ec5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10397,6 +10397,11 @@ namespace ts {
return type;
}

function maybeCloneTypeParameter(p: TypeParameter) {
const constraint = getConstraintOfTypeParameter(p);
return constraint && maybeTypeOfKind(constraint, TypeFlags.Instantiable | TypeFlags.GenericMappedType) ? cloneTypeParameter(p) : p;
}

function getConditionalType(root: ConditionalRoot, mapper: TypeMapper | undefined): Type {
const checkType = instantiateType(root.checkType, mapper);
const extendsType = instantiateType(root.extendsType, mapper);
Expand All @@ -10423,11 +10428,14 @@ namespace ts {
// * The original `mapper` used to create this conditional
// * The mapper that maps the old root type parameter to the clone (`freshMapper`)
// * The mapper that maps the clone to its inference result (`context.mapper`)
const freshParams = map(root.inferTypeParameters, cloneTypeParameter);
const freshParams = map(root.inferTypeParameters, maybeCloneTypeParameter);
const freshMapper = createTypeMapper(root.inferTypeParameters, freshParams);
const context = createInferenceContext(freshParams, /*signature*/ undefined, InferenceFlags.None);
const freshCombinedMapper = combineTypeMappers(mapper, freshMapper);
for (const p of freshParams) {
p.mapper = combineTypeMappers(mapper, freshMapper);
if (root.inferTypeParameters.indexOf(p) === -1) {
p.mapper = freshCombinedMapper;
}
}
if (!checkTypeInstantiable) {
// We don't want inferences from constraints as they may cause us to eagerly resolve the
Expand Down

0 comments on commit 9133ec5

Please sign in to comment.