Skip to content

Commit 0165e80

Browse files
committed
Don't widen contextually typed literals in initializer expressions
1 parent b8def16 commit 0165e80

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4919,7 +4919,7 @@ namespace ts {
49194919
if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkDeclarationInitializer(declaration)) & TypeFlags.Undefined)) {
49204920
type = getTypeWithFacts(type, TypeFacts.NEUndefined);
49214921
}
4922-
return declaration.initializer && !getContextualTypeForVariableLikeDeclaration(walkUpBindingElementsAndPatterns(declaration)) ?
4922+
return declaration.initializer && !getEffectiveTypeAnnotationNode(walkUpBindingElementsAndPatterns(declaration)) ?
49234923
getUnionType([type, checkDeclarationInitializer(declaration)], UnionReduction.Subtype) :
49244924
type;
49254925
}
@@ -22810,7 +22810,8 @@ namespace ts {
2281022810
const type = getTypeOfExpression(initializer, /*cache*/ true);
2281122811
const widened = getCombinedNodeFlags(declaration) & NodeFlags.Const ||
2281222812
isDeclarationReadonly(declaration) ||
22813-
isTypeAssertion(initializer) ? type : getWidenedLiteralType(type);
22813+
isTypeAssertion(initializer) ||
22814+
isLiteralOfContextualType(type, getContextualType(initializer)) ? type : getWidenedLiteralType(type);
2281422815
if (isInJSFile(declaration)) {
2281522816
if (widened.flags & TypeFlags.Nullable) {
2281622817
reportImplicitAny(declaration, anyType);

0 commit comments

Comments
 (0)