mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 21:36:50 -05:00
Limit the narrow-to-fresh rule added with boolean literals to only boolean literals (#27274)
* Remove the narrow-to-fresh rule added with boolean literals
* Revert "Remove the narrow-to-fresh rule added with boolean literals"
This reverts commit 9f96fe5da3.
* Only apply freshness to booleans for now
* Add largeish example from issue
* Should be AND not OR
* Add minor improvements suggested by @ahejelsberg
* Reorder conditional a bit
This commit is contained in:
@@ -8873,7 +8873,7 @@ namespace ts {
|
||||
}
|
||||
switch (unionReduction) {
|
||||
case UnionReduction.Literal:
|
||||
if (includes & TypeFlags.StringOrNumberLiteralOrUnique) {
|
||||
if (includes & TypeFlags.StringOrNumberLiteralOrUnique | TypeFlags.BooleanLiteral) {
|
||||
removeRedundantLiteralTypes(typeSet, includes);
|
||||
}
|
||||
break;
|
||||
@@ -12938,8 +12938,8 @@ namespace ts {
|
||||
function getDefinitelyFalsyPartOfType(type: Type): Type {
|
||||
return type.flags & TypeFlags.String ? emptyStringType :
|
||||
type.flags & TypeFlags.Number ? zeroType :
|
||||
type.flags & TypeFlags.Boolean || type === regularFalseType ? regularFalseType :
|
||||
type === falseType ? falseType :
|
||||
type === regularFalseType ||
|
||||
type === falseType ||
|
||||
type.flags & (TypeFlags.Void | TypeFlags.Undefined | TypeFlags.Null) ||
|
||||
type.flags & TypeFlags.StringLiteral && (<LiteralType>type).value === "" ||
|
||||
type.flags & TypeFlags.NumberLiteral && (<LiteralType>type).value === 0 ? type :
|
||||
@@ -14214,7 +14214,7 @@ namespace ts {
|
||||
return assignedType;
|
||||
}
|
||||
let reducedType = filterType(declaredType, t => typeMaybeAssignableTo(assignedType, t));
|
||||
if (assignedType.flags & (TypeFlags.FreshLiteral | TypeFlags.Literal)) {
|
||||
if (assignedType.flags & TypeFlags.FreshLiteral && assignedType.flags & TypeFlags.BooleanLiteral) {
|
||||
reducedType = mapType(reducedType, getFreshTypeOfLiteralType); // Ensure that if the assignment is a fresh type, that we narrow to fresh types
|
||||
}
|
||||
// Our crude heuristic produces an invalid result in some cases: see GH#26130.
|
||||
|
||||
Reference in New Issue
Block a user