Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into release-5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
typescript-bot committed Jan 28, 2024
2 parents a450bda + 36f9e9e commit d3f2a17
Show file tree
Hide file tree
Showing 28 changed files with 5,801 additions and 1,599 deletions.
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 14 additions & 7 deletions src/compiler/checker.ts
Expand Up @@ -17119,19 +17119,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function removeStringLiteralsMatchedByTemplateLiterals(types: Type[]) {
const templates = filter(types, t => !!(t.flags & TypeFlags.TemplateLiteral) && isPatternLiteralType(t)) as TemplateLiteralType[];
const templates = filter(types, isPatternLiteralType) as (TemplateLiteralType | StringMappingType)[];
if (templates.length) {
let i = types.length;
while (i > 0) {
i--;
const t = types[i];
if (t.flags & TypeFlags.StringLiteral && some(templates, template => isTypeMatchedByTemplateLiteralType(t, template))) {
if (t.flags & TypeFlags.StringLiteral && some(templates, template => isTypeMatchedByTemplateLiteralOrStringMapping(t, template))) {
orderedRemoveItemAt(types, i);
}
}
}
}

function isTypeMatchedByTemplateLiteralOrStringMapping(type: Type, template: TemplateLiteralType | StringMappingType) {
return template.flags & TypeFlags.TemplateLiteral ?
isTypeMatchedByTemplateLiteralType(type, template as TemplateLiteralType) :
isMemberOfStringMapping(type, template);
}

function removeConstrainedTypeVariables(types: Type[]) {
const typeVariables: TypeVariable[] = [];
// First collect a list of the type variables occurring in constraining intersections.
Expand Down Expand Up @@ -17246,7 +17252,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (includes & (TypeFlags.Enum | TypeFlags.Literal | TypeFlags.UniqueESSymbol | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) || includes & TypeFlags.Void && includes & TypeFlags.Undefined) {
removeRedundantLiteralTypes(typeSet, includes, !!(unionReduction & UnionReduction.Subtype));
}
if (includes & TypeFlags.StringLiteral && includes & TypeFlags.TemplateLiteral) {
if (includes & TypeFlags.StringLiteral && includes & (TypeFlags.TemplateLiteral | TypeFlags.StringMapping)) {
removeStringLiteralsMatchedByTemplateLiterals(typeSet);
}
if (includes & TypeFlags.IncludesConstrainedTypeVariable) {
Expand Down Expand Up @@ -17442,18 +17448,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

/**
* Returns `true` if the intersection of the template literals and string literals is the empty set, eg `get${string}` & "setX", and should reduce to `never`
* Returns true if the intersection of the template literals and string literals is the empty set,
* for example `get${string}` & "setX", and should reduce to never.
*/
function extractRedundantTemplateLiterals(types: Type[]): boolean {
let i = types.length;
const literals = filter(types, t => !!(t.flags & TypeFlags.StringLiteral));
while (i > 0) {
i--;
const t = types[i];
if (!(t.flags & TypeFlags.TemplateLiteral)) continue;
if (!(t.flags & (TypeFlags.TemplateLiteral | TypeFlags.StringMapping))) continue;
for (const t2 of literals) {
if (isTypeSubtypeOf(t2, t)) {
// eg, ``get${T}` & "getX"` is just `"getX"`
// For example, `get${T}` & "getX" is just "getX", and Lowercase<string> & "foo" is just "foo"
orderedRemoveItemAt(types, i);
break;
}
Expand Down Expand Up @@ -17563,7 +17570,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
) {
return neverType;
}
if (includes & TypeFlags.TemplateLiteral && includes & TypeFlags.StringLiteral && extractRedundantTemplateLiterals(typeSet)) {
if (includes & (TypeFlags.TemplateLiteral | TypeFlags.StringMapping) && includes & TypeFlags.StringLiteral && extractRedundantTemplateLiterals(typeSet)) {
return neverType;
}
if (includes & TypeFlags.Any) {
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/types.ts
Expand Up @@ -6125,6 +6125,8 @@ export const enum TypeFlags {
NonPrimitive = 1 << 26, // intrinsic object type
TemplateLiteral = 1 << 27, // Template literal type
StringMapping = 1 << 28, // Uppercase/Lowercase type
/** @internal */
Reserved1 = 1 << 29, // Used by union/intersection type construction

/** @internal */
AnyOrUnknown = Any | Unknown,
Expand Down Expand Up @@ -6172,7 +6174,7 @@ export const enum TypeFlags {
Narrowable = Any | Unknown | StructuredOrInstantiable | StringLike | NumberLike | BigIntLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive,
// The following flags are aggregated during union and intersection type construction
/** @internal */
IncludesMask = Any | Unknown | Primitive | Never | Object | Union | Intersection | NonPrimitive | TemplateLiteral,
IncludesMask = Any | Unknown | Primitive | Never | Object | Union | Intersection | NonPrimitive | TemplateLiteral | StringMapping,
// The following flags are used for different purposes during union and intersection type construction
/** @internal */
IncludesMissingType = TypeParameter,
Expand All @@ -6185,7 +6187,7 @@ export const enum TypeFlags {
/** @internal */
IncludesInstantiable = Substitution,
/** @internal */
IncludesConstrainedTypeVariable = StringMapping,
IncludesConstrainedTypeVariable = Reserved1,
/** @internal */
NotPrimitiveUnion = Any | Unknown | Void | Never | Object | Intersection | IncludesInstantiable,
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/protocol.ts
Expand Up @@ -211,7 +211,7 @@ export interface Request extends Message {
/**
* Request to reload the project structure for all the opened files
*/
export interface ReloadProjectsRequest extends Message {
export interface ReloadProjectsRequest extends Request {
command: CommandTypes.ReloadProjects;
}

Expand Down

0 comments on commit d3f2a17

Please sign in to comment.