Skip to content

Commit 420e58c

Browse files
committed
Implicit constraints in non-distributive '[T] extends [U] ? X : Y' types
1 parent 56710dd commit 420e58c

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7349,13 +7349,24 @@ namespace ts {
73497349
return result;
73507350
}
73517351

7352+
function isUnaryTupleTypeNode(node: TypeNode) {
7353+
return node.kind === SyntaxKind.TupleType && (<TupleTypeNode>node).elementTypes.length === 1;
7354+
}
7355+
7356+
function getImpliedConstraint(typeVariable: TypeVariable, checkNode: TypeNode, extendsNode: TypeNode): Type {
7357+
return isUnaryTupleTypeNode(checkNode) && isUnaryTupleTypeNode(extendsNode) ? getImpliedConstraint(typeVariable, (<TupleTypeNode>checkNode).elementTypes[0], (<TupleTypeNode>extendsNode).elementTypes[0]) :
7358+
getActualTypeVariable(getTypeFromTypeNode(checkNode)) === typeVariable ? getTypeFromTypeNode(extendsNode) :
7359+
undefined;
7360+
}
7361+
73527362
function getConstrainedTypeVariable(typeVariable: TypeVariable, node: Node) {
73537363
let constraints: Type[];
73547364
while (isPartOfTypeNode(node)) {
73557365
const parent = node.parent;
73567366
if (parent.kind === SyntaxKind.ConditionalType && node === (<ConditionalTypeNode>parent).trueType) {
7357-
if (getActualTypeVariable(getTypeFromTypeNode((<ConditionalTypeNode>parent).checkType)) === typeVariable) {
7358-
constraints = append(constraints, getTypeFromTypeNode((<ConditionalTypeNode>parent).extendsType));
7367+
const constraint = getImpliedConstraint(typeVariable, (<ConditionalTypeNode>parent).checkType, (<ConditionalTypeNode>parent).extendsType);
7368+
if (constraint) {
7369+
constraints = append(constraints, constraint);
73597370
}
73607371
}
73617372
node = parent;

0 commit comments

Comments
 (0)