Merge pull request #31337 from microsoft/fixConditionalTypeParameterReference

Fix type parameter leakage in conditional types
This commit is contained in:
Anders Hejlsberg
2019-05-10 13:11:45 -07:00
committed by GitHub
6 changed files with 69 additions and 18 deletions

View File

@@ -10482,21 +10482,6 @@ namespace ts {
return result;
}
function isPossiblyReferencedInConditionalType(tp: TypeParameter, node: Node) {
if (isTypeParameterPossiblyReferenced(tp, node)) {
return true;
}
while (node) {
if (node.kind === SyntaxKind.ConditionalType) {
if (isTypeParameterPossiblyReferenced(tp, (<ConditionalTypeNode>node).extendsType)) {
return true;
}
}
node = node.parent;
}
return false;
}
function getTypeFromConditionalTypeNode(node: ConditionalTypeNode): Type {
const links = getNodeLinks(node);
if (!links.resolvedType) {
@@ -10504,7 +10489,7 @@ namespace ts {
const aliasSymbol = getAliasSymbolForTypeNode(node);
const aliasTypeArguments = getTypeArgumentsForAliasSymbol(aliasSymbol);
const allOuterTypeParameters = getOuterTypeParameters(node, /*includeThisTypes*/ true);
const outerTypeParameters = aliasTypeArguments ? allOuterTypeParameters : filter(allOuterTypeParameters, tp => isPossiblyReferencedInConditionalType(tp, node));
const outerTypeParameters = aliasTypeArguments ? allOuterTypeParameters : filter(allOuterTypeParameters, tp => isTypeParameterPossiblyReferenced(tp, node));
const root: ConditionalRoot = {
node,
checkType,
@@ -11198,9 +11183,12 @@ namespace ts {
// type parameter, or if the node contains type queries, we consider the type parameter possibly referenced.
if (tp.symbol && tp.symbol.declarations && tp.symbol.declarations.length === 1) {
const container = tp.symbol.declarations[0].parent;
if (findAncestor(node, n => n.kind === SyntaxKind.Block ? "quit" : n === container)) {
return !!forEachChild(node, containsReference);
for (let n = node; n !== container; n = n.parent) {
if (!n || n.kind === SyntaxKind.Block || n.kind === SyntaxKind.ConditionalType && forEachChild((<ConditionalTypeNode>n).extendsType, containsReference)) {
return true;
}
}
return !!forEachChild(node, containsReference);
}
return true;
function containsReference(node: Node): boolean {