Template literal types for contextually typed template literal expressions (#43376)

* Template literal types for contextually typed template literal expressions

* Accept new baselines

* Add regression test

* Add a few more tests
This commit is contained in:
Anders Hejlsberg
2021-03-26 10:32:05 -07:00
committed by GitHub
parent b34e705672
commit a26acf4540
9 changed files with 199 additions and 87 deletions

View File

@@ -31824,7 +31824,12 @@ namespace ts {
texts.push(span.literal.text);
types.push(isTypeAssignableTo(type, templateConstraintType) ? type : stringType);
}
return isConstContext(node) ? getTemplateLiteralType(texts, types) : stringType;
return isConstContext(node) || someType(getContextualType(node) || unknownType, isTemplateLiteralContextualType) ? getTemplateLiteralType(texts, types) : stringType;
}
function isTemplateLiteralContextualType(type: Type): boolean {
return !!(type.flags & (TypeFlags.StringLiteral | TypeFlags.TemplateLiteral) ||
type.flags & TypeFlags.InstantiableNonPrimitive && maybeTypeOfKind(getBaseConstraintOfType(type) || unknownType, TypeFlags.StringLike));
}
function getContextNode(node: Expression): Node {