-
Notifications
You must be signed in to change notification settings - Fork 12.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve literal types in contextual unions #19966
Changes from 1 commit
9b2a760
f7bbb72
5e89e55
d1d437d
c2b43ee
ba34d9d
12ce050
a39515f
2fa2373
a381bc8
300816f
1683caf
c928f41
85bfe16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4306,9 +4306,10 @@ namespace ts { | |
if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkExpressionCached(declaration.initializer)) & TypeFlags.Undefined)) { | ||
type = getTypeWithFacts(type, TypeFacts.NEUndefined); | ||
} | ||
return declaration.initializer ? | ||
type = declaration.initializer ? | ||
getUnionType([type, checkExpressionCached(declaration.initializer)], /*subtypeReduction*/ true) : | ||
type; | ||
return shouldUseLiteralType(declaration) ? type : getWidenedLiteralType(type); | ||
} | ||
|
||
function getTypeForDeclarationFromJSDocComment(declaration: Node) { | ||
|
@@ -10672,7 +10673,7 @@ namespace ts { | |
} | ||
|
||
function getWidenedLiteralLikeTypeForContextualType(type: Type, contextualType: Type) { | ||
if (!isLiteralLikeContextualType(contextualType)) { | ||
if (!isLiteralLikeContextualType(type, contextualType)) { | ||
type = getWidenedUniqueESSymbolType(getWidenedLiteralType(type)); | ||
} | ||
return type; | ||
|
@@ -18856,24 +18857,30 @@ namespace ts { | |
|
||
function checkDeclarationInitializer(declaration: VariableLikeDeclaration) { | ||
const type = getTypeOfExpression(declaration.initializer, /*cache*/ true); | ||
return shouldUseLiteralType(declaration) ? type : getWidenedLiteralType(type); | ||
} | ||
|
||
function shouldUseLiteralType(declaration: VariableLikeDeclaration) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return getCombinedNodeFlags(declaration) & NodeFlags.Const || | ||
(getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration)) || | ||
isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type); | ||
getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration) || | ||
(declaration.initializer && isTypeAssertion(declaration.initializer)); | ||
} | ||
|
||
function isLiteralLikeContextualType(contextualType: Type) { | ||
function isLiteralLikeContextualType(candidateLiteral: Type, contextualType: Type): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (contextualType) { | ||
if (contextualType.flags & TypeFlags.UnionOrIntersection) { | ||
return some((contextualType as UnionOrIntersectionType).types, t => isLiteralLikeContextualType(candidateLiteral, t)); | ||
} | ||
if (contextualType.flags & TypeFlags.TypeVariable) { | ||
const constraint = getBaseConstraintOfType(contextualType) || emptyObjectType; | ||
// If the type parameter is constrained to the base primitive type we're checking for, | ||
// consider this a literal context. For example, given a type parameter 'T extends string', | ||
// this causes us to infer string literal types for T. | ||
if (constraint.flags & (TypeFlags.String | TypeFlags.Number | TypeFlags.Boolean | TypeFlags.Enum | TypeFlags.ESSymbol)) { | ||
return true; | ||
} | ||
contextualType = constraint; | ||
} | ||
return maybeTypeOfKind(contextualType, (TypeFlags.Literal | TypeFlags.Index | TypeFlags.UniqueESSymbol)); | ||
return isLiteralLikeContextualType(candidateLiteral, constraint); | ||
} | ||
// No need to `maybeTypeOfKind` on the contextual type, as it can't be a union, _however_, `candidateLiteral` might still be one! | ||
return !!(((contextualType.flags & TypeFlags.StringLike) && maybeTypeOfKind(candidateLiteral, TypeFlags.StringLike)) || | ||
((contextualType.flags & TypeFlags.NumberLike) && maybeTypeOfKind(candidateLiteral, TypeFlags.NumberLike)) || | ||
((contextualType.flags & TypeFlags.BooleanLike) && maybeTypeOfKind(candidateLiteral, TypeFlags.BooleanLike)) || | ||
((contextualType.flags & TypeFlags.ESSymbolLike) && maybeTypeOfKind(candidateLiteral, TypeFlags.ESSymbolLike)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why all the extra parentheses? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Old habit; I tend to add parens around any inner binary expression since internalizing operator precedence beyond simple PEMDAS isn't something I, personally, bother to think about. They're gone now~ |
||
); | ||
} | ||
return false; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the cases that are affected by this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can see the changes at https://github.com/weswigham/TypeScript/pull/1/files