@@ -6233,7 +6233,12 @@ namespace ts {
6233
6233
}
6234
6234
6235
6235
function getDefaultConstraintOfConditionalType(type: ConditionalType) {
6236
- return getUnionType([getInferredTrueTypeFromConditionalType(type), getFalseTypeFromConditionalType(type)]);
6236
+ if (!type.resolvedDefaultConstraint) {
6237
+ const rootTrueType = type.root.trueType;
6238
+ const rootTrueConstraint = rootTrueType.flags & TypeFlags.Substitution ? (<SubstitutionType>rootTrueType).substitute : rootTrueType;
6239
+ type.resolvedDefaultConstraint = getUnionType([instantiateType(rootTrueConstraint, type.combinedMapper || type.mapper), getFalseTypeFromConditionalType(type)]);
6240
+ }
6241
+ return type.resolvedDefaultConstraint;
6237
6242
}
6238
6243
6239
6244
function getConstraintOfDistributiveConditionalType(type: ConditionalType): Type {
@@ -6246,7 +6251,10 @@ namespace ts {
6246
6251
const constraint = getConstraintOfType(type.checkType);
6247
6252
if (constraint) {
6248
6253
const mapper = createTypeMapper([<TypeParameter>type.root.checkType], [constraint]);
6249
- return getConditionalTypeInstantiation(type, combineTypeMappers(mapper, type.mapper));
6254
+ const instantiated = getConditionalTypeInstantiation(type, combineTypeMappers(mapper, type.mapper));
6255
+ if (!(instantiated.flags & TypeFlags.Never)) {
6256
+ return instantiated;
6257
+ }
6250
6258
}
6251
6259
}
6252
6260
return undefined;
@@ -8398,12 +8406,6 @@ namespace ts {
8398
8406
return type.resolvedFalseType || (type.resolvedFalseType = instantiateType(type.root.falseType, type.mapper));
8399
8407
}
8400
8408
8401
- function getInferredTrueTypeFromConditionalType(type: ConditionalType) {
8402
- return type.combinedMapper ?
8403
- type.resolvedInferredTrueType || (type.resolvedInferredTrueType = instantiateType(type.root.trueType, type.combinedMapper)) :
8404
- getTrueTypeFromConditionalType(type);
8405
- }
8406
-
8407
8409
function getInferTypeParameters(node: ConditionalTypeNode): TypeParameter[] {
8408
8410
let result: TypeParameter[];
8409
8411
if (node.locals) {
@@ -8416,13 +8418,25 @@ namespace ts {
8416
8418
return result;
8417
8419
}
8418
8420
8421
+ function getTopConditionalType(node: Node): ConditionalTypeNode {
8422
+ let result: ConditionalTypeNode;
8423
+ while (node) {
8424
+ if (node.kind === SyntaxKind.ConditionalType) {
8425
+ result = <ConditionalTypeNode>node;
8426
+ }
8427
+ node = node.parent;
8428
+ }
8429
+ return result;
8430
+ }
8431
+
8419
8432
function getTypeFromConditionalTypeNode(node: ConditionalTypeNode): Type {
8420
8433
const links = getNodeLinks(node);
8421
8434
if (!links.resolvedType) {
8422
8435
const checkType = getTypeFromTypeNode(node.checkType);
8423
8436
const aliasTypeArguments = getAliasTypeArgumentsForTypeNode(node);
8424
8437
const allOuterTypeParameters = getOuterTypeParameters(node, /*includeThisTypes*/ true);
8425
- const outerTypeParameters = aliasTypeArguments ? allOuterTypeParameters : filter(allOuterTypeParameters, tp => isTypeParameterPossiblyReferenced(tp, node));
8438
+ const topNode = getTopConditionalType(node);
8439
+ const outerTypeParameters = aliasTypeArguments ? allOuterTypeParameters : filter(allOuterTypeParameters, tp => isTypeParameterPossiblyReferenced(tp, topNode));
8426
8440
const root: ConditionalRoot = {
8427
8441
node,
8428
8442
checkType,
0 commit comments