mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-17 11:24:29 -05:00
Merge pull request #11587 from Microsoft/narrowStringAndNumber
Narrow string and number types in literal equality checks
This commit is contained in:
@@ -8461,6 +8461,28 @@ namespace ts {
|
||||
return f(type) ? type : neverType;
|
||||
}
|
||||
|
||||
function mapType(type: Type, f: (t: Type) => Type): Type {
|
||||
return type.flags & TypeFlags.Union ? getUnionType(map((<UnionType>type).types, f)) : f(type);
|
||||
}
|
||||
|
||||
function extractTypesOfKind(type: Type, kind: TypeFlags) {
|
||||
return filterType(type, t => (t.flags & kind) !== 0);
|
||||
}
|
||||
|
||||
// Return a new type in which occurrences of the string and number primitive types in
|
||||
// typeWithPrimitives have been replaced with occurrences of string literals and numeric
|
||||
// literals in typeWithLiterals, respectively.
|
||||
function replacePrimitivesWithLiterals(typeWithPrimitives: Type, typeWithLiterals: Type) {
|
||||
if (isTypeSubsetOf(stringType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, TypeFlags.StringLiteral) ||
|
||||
isTypeSubsetOf(numberType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, TypeFlags.NumberLiteral)) {
|
||||
return mapType(typeWithPrimitives, t =>
|
||||
t.flags & TypeFlags.String ? extractTypesOfKind(typeWithLiterals, TypeFlags.String | TypeFlags.StringLiteral) :
|
||||
t.flags & TypeFlags.Number ? extractTypesOfKind(typeWithLiterals, TypeFlags.Number | TypeFlags.NumberLiteral) :
|
||||
t);
|
||||
}
|
||||
return typeWithPrimitives;
|
||||
}
|
||||
|
||||
function isIncomplete(flowType: FlowType) {
|
||||
return flowType.flags === 0;
|
||||
}
|
||||
@@ -8796,7 +8818,7 @@ namespace ts {
|
||||
}
|
||||
if (assumeTrue) {
|
||||
const narrowedType = filterType(type, t => areTypesComparable(t, valueType));
|
||||
return narrowedType.flags & TypeFlags.Never ? type : narrowedType;
|
||||
return narrowedType.flags & TypeFlags.Never ? type : replacePrimitivesWithLiterals(narrowedType, valueType);
|
||||
}
|
||||
if (isUnitType(valueType)) {
|
||||
const regularType = getRegularTypeOfLiteralType(valueType);
|
||||
@@ -8843,7 +8865,9 @@ namespace ts {
|
||||
const clauseTypes = switchTypes.slice(clauseStart, clauseEnd);
|
||||
const hasDefaultClause = clauseStart === clauseEnd || contains(clauseTypes, neverType);
|
||||
const discriminantType = getUnionType(clauseTypes);
|
||||
const caseType = discriminantType.flags & TypeFlags.Never ? neverType : filterType(type, t => isTypeComparableTo(discriminantType, t));
|
||||
const caseType =
|
||||
discriminantType.flags & TypeFlags.Never ? neverType :
|
||||
replacePrimitivesWithLiterals(filterType(type, t => isTypeComparableTo(discriminantType, t)), discriminantType);
|
||||
if (!hasDefaultClause) {
|
||||
return caseType;
|
||||
}
|
||||
|
||||
@@ -1190,7 +1190,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
function scanBinaryOrOctalDigits(base: number): number {
|
||||
Debug.assert(base !== 2 || base !== 8, "Expected either base 2 or base 8");
|
||||
Debug.assert(base === 2 || base === 8, "Expected either base 2 or base 8");
|
||||
|
||||
let value = 0;
|
||||
// For counting number of digits; Valid binaryIntegerLiteral must have at least one binary digit following B or b.
|
||||
|
||||
@@ -2644,7 +2644,7 @@ namespace ts {
|
||||
// 'Narrowable' types are types where narrowing actually narrows.
|
||||
// This *should* be every type other than null, undefined, void, and never
|
||||
Narrowable = Any | StructuredType | TypeParameter | StringLike | NumberLike | BooleanLike | ESSymbol,
|
||||
NotUnionOrUnit = Any | String | Number | ESSymbol | ObjectType,
|
||||
NotUnionOrUnit = Any | ESSymbol | ObjectType,
|
||||
/* @internal */
|
||||
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
|
||||
/* @internal */
|
||||
|
||||
Reference in New Issue
Block a user