@@ -12041,7 +12041,7 @@ namespace ts {
12041
12041
else if (type !== firstType) {
12042
12042
checkFlags |= CheckFlags.HasNonUniformType;
12043
12043
}
12044
- if (isLiteralType(type)) {
12044
+ if (isLiteralType(type) || isPatternLiteralType(type) ) {
12045
12045
checkFlags |= CheckFlags.HasLiteralType;
12046
12046
}
12047
12047
if (type.flags & TypeFlags.Never) {
@@ -19015,6 +19015,9 @@ namespace ts {
19015
19015
}
19016
19016
else if (target.flags & TypeFlags.TemplateLiteral) {
19017
19017
if (source.flags & TypeFlags.TemplateLiteral) {
19018
+ if (relation === comparableRelation) {
19019
+ return templateLiteralTypesDefinitelyUnrelated(source as TemplateLiteralType, target as TemplateLiteralType) ? Ternary.False : Ternary.True;
19020
+ }
19018
19021
// Report unreliable variance for type variables referenced in template literal type placeholders.
19019
19022
// For example, `foo-${number}` is related to `foo-${string}` even though number isn't related to string.
19020
19023
instantiateType(source, makeFunctionTypeMapper(reportUnreliableMarkers));
@@ -19054,11 +19057,10 @@ namespace ts {
19054
19057
return result;
19055
19058
}
19056
19059
}
19057
- else if (source.flags & TypeFlags.TemplateLiteral) {
19060
+ else if (source.flags & TypeFlags.TemplateLiteral && !(target.flags & TypeFlags.Object) ) {
19058
19061
if (!(target.flags & TypeFlags.TemplateLiteral)) {
19059
- const baseConstraint = getBaseConstraintOfType(source);
19060
- const constraint = baseConstraint && baseConstraint !== source ? baseConstraint : stringType;
19061
- if (result = isRelatedTo(constraint, target, reportErrors)) {
19062
+ const constraint = getBaseConstraintOfType(source);
19063
+ if (constraint && constraint !== source && (result = isRelatedTo(constraint, target, reportErrors))) {
19062
19064
resetErrorInfo(saveErrorInfo);
19063
19065
return result;
19064
19066
}
@@ -21429,6 +21431,18 @@ namespace ts {
21429
21431
return !!(type.symbol && some(type.symbol.declarations, hasSkipDirectInferenceFlag));
21430
21432
}
21431
21433
21434
+ function templateLiteralTypesDefinitelyUnrelated(source: TemplateLiteralType, target: TemplateLiteralType) {
21435
+ // Two template literal types with diffences in their starting or ending text spans are definitely unrelated.
21436
+ const sourceStart = source.texts[0];
21437
+ const targetStart = target.texts[0];
21438
+ const sourceEnd = source.texts[source.texts.length - 1];
21439
+ const targetEnd = target.texts[target.texts.length - 1];
21440
+ const startLen = Math.min(sourceStart.length, targetStart.length);
21441
+ const endLen = Math.min(sourceEnd.length, targetEnd.length);
21442
+ return sourceStart.slice(0, startLen) !== targetStart.slice(0, startLen) ||
21443
+ sourceEnd.slice(sourceEnd.length - endLen) !== targetEnd.slice(targetEnd.length - endLen);
21444
+ }
21445
+
21432
21446
function isValidBigIntString(s: string): boolean {
21433
21447
const scanner = createScanner(ScriptTarget.ESNext, /*skipTrivia*/ false);
21434
21448
let success = true;
@@ -22529,7 +22543,7 @@ namespace ts {
22529
22543
if ((prop as TransientSymbol).isDiscriminantProperty === undefined) {
22530
22544
(prop as TransientSymbol).isDiscriminantProperty =
22531
22545
((prop as TransientSymbol).checkFlags & CheckFlags.Discriminant) === CheckFlags.Discriminant &&
22532
- !maybeTypeOfKind (getTypeOfSymbol(prop), TypeFlags.Instantiable & ~TypeFlags.TemplateLiteral );
22546
+ !isGenericType (getTypeOfSymbol(prop));
22533
22547
}
22534
22548
return !!(prop as TransientSymbol).isDiscriminantProperty;
22535
22549
}
@@ -23088,15 +23102,17 @@ namespace ts {
23088
23102
return filterType(type, t => (t.flags & kind) !== 0);
23089
23103
}
23090
23104
23091
- // Return a new type in which occurrences of the string and number primitive types in
23092
- // typeWithPrimitives have been replaced with occurrences of string literals and numeric
23093
- // literals in typeWithLiterals, respectively.
23105
+ // Return a new type in which occurrences of the string, number and bigint primitives and placeholder template
23106
+ // literal types in typeWithPrimitives have been replaced with occurrences of compatible and more specific types
23107
+ // from typeWithLiterals. This is essentially a limited form of intersection between the two types. We avoid a
23108
+ // true intersection because it is more costly and, when applied to union types, generates a large number of
23109
+ // types we don't actually care about.
23094
23110
function replacePrimitivesWithLiterals(typeWithPrimitives: Type, typeWithLiterals: Type) {
23095
- if (isTypeSubsetOf(stringType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, TypeFlags.StringLiteral) ||
23096
- isTypeSubsetOf(numberType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, TypeFlags.NumberLiteral) ||
23097
- isTypeSubsetOf(bigintType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, TypeFlags.BigIntLiteral)) {
23111
+ if (maybeTypeOfKind(typeWithPrimitives, TypeFlags.String | TypeFlags.TemplateLiteral | TypeFlags.Number | TypeFlags.BigInt) &&
23112
+ maybeTypeOfKind(typeWithLiterals, TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping | TypeFlags.NumberLiteral | TypeFlags.BigIntLiteral)) {
23098
23113
return mapType(typeWithPrimitives, t =>
23099
- t.flags & TypeFlags.String ? extractTypesOfKind(typeWithLiterals, TypeFlags.String | TypeFlags.StringLiteral) :
23114
+ t.flags & TypeFlags.String ? extractTypesOfKind(typeWithLiterals, TypeFlags.String | TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) :
23115
+ isPatternLiteralType(t) && !maybeTypeOfKind(typeWithLiterals, TypeFlags.String | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) ? extractTypesOfKind(typeWithLiterals, TypeFlags.StringLiteral) :
23100
23116
t.flags & TypeFlags.Number ? extractTypesOfKind(typeWithLiterals, TypeFlags.Number | TypeFlags.NumberLiteral) :
23101
23117
t.flags & TypeFlags.BigInt ? extractTypesOfKind(typeWithLiterals, TypeFlags.BigInt | TypeFlags.BigIntLiteral) : t);
23102
23118
}
@@ -23939,7 +23955,7 @@ namespace ts {
23939
23955
const narrowedPropType = narrowType(propType);
23940
23956
return filterType(type, t => {
23941
23957
const discriminantType = getTypeOfPropertyOrIndexSignature(t, propName);
23942
- return !(discriminantType .flags & TypeFlags.Never) && isTypeComparableTo(discriminantType, narrowedPropType );
23958
+ return !(narrowedPropType .flags & TypeFlags.Never) && isTypeComparableTo(narrowedPropType, discriminantType );
23943
23959
});
23944
23960
}
23945
23961
@@ -31095,16 +31111,14 @@ namespace ts {
31095
31111
}
31096
31112
31097
31113
function checkImportMetaProperty(node: MetaProperty) {
31098
- if (moduleKind !== ModuleKind.ES2020 && moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System) {
31099
- if (moduleKind === ModuleKind.Node12 || moduleKind === ModuleKind.NodeNext) {
31100
- if (getSourceFileOfNode(node).impliedNodeFormat !== ModuleKind.ESNext) {
31101
- error(node, Diagnostics.The_import_meta_meta_property_is_not_allowed_in_files_which_will_build_into_CommonJS_output);
31102
- }
31103
- }
31104
- else {
31105
- error(node, Diagnostics.The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_es2020_esnext_system_node12_or_nodenext);
31114
+ if (moduleKind === ModuleKind.Node12 || moduleKind === ModuleKind.NodeNext) {
31115
+ if (getSourceFileOfNode(node).impliedNodeFormat !== ModuleKind.ESNext) {
31116
+ error(node, Diagnostics.The_import_meta_meta_property_is_not_allowed_in_files_which_will_build_into_CommonJS_output);
31106
31117
}
31107
31118
}
31119
+ else if (moduleKind < ModuleKind.ES2020 && moduleKind !== ModuleKind.System) {
31120
+ error(node, Diagnostics.The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_es2020_es2022_esnext_system_node12_or_nodenext);
31121
+ }
31108
31122
const file = getSourceFileOfNode(node);
31109
31123
Debug.assert(!!(file.flags & NodeFlags.PossiblyContainsImportMeta), "Containing file is missing import meta node flag.");
31110
31124
return node.name.escapedText === "meta" ? getGlobalImportMetaType() : errorType;
@@ -32126,10 +32140,10 @@ namespace ts {
32126
32140
Diagnostics.await_expressions_are_only_allowed_at_the_top_level_of_a_file_when_that_file_is_a_module_but_this_file_has_no_imports_or_exports_Consider_adding_an_empty_export_to_make_this_file_a_module);
32127
32141
diagnostics.add(diagnostic);
32128
32142
}
32129
- if ((moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System && !(moduleKind === ModuleKind.NodeNext && getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.ESNext)) || languageVersion < ScriptTarget.ES2017) {
32143
+ if ((moduleKind !== ModuleKind.ES2022 && moduleKind !== ModuleKind. ESNext && moduleKind !== ModuleKind.System && !(moduleKind === ModuleKind.NodeNext && getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.ESNext)) || languageVersion < ScriptTarget.ES2017) {
32130
32144
span = getSpanOfTokenAtPosition(sourceFile, node.pos);
32131
32145
const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length,
32132
- Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_esnext_system_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher );
32146
+ Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher );
32133
32147
diagnostics.add(diagnostic);
32134
32148
}
32135
32149
}
@@ -33723,7 +33737,7 @@ namespace ts {
33723
33737
}
33724
33738
33725
33739
function checkParenthesizedExpression(node: ParenthesizedExpression, checkMode?: CheckMode): Type {
33726
- if (isJSDocTypeAssertion(node)) {
33740
+ if (hasJSDocNodes(node) && isJSDocTypeAssertion(node)) {
33727
33741
const type = getJSDocTypeAssertionType(node);
33728
33742
return checkAssertionWorker(type, type, node.expression, checkMode);
33729
33743
}
@@ -42792,9 +42806,9 @@ namespace ts {
42792
42806
diagnostics.add(createDiagnosticForNode(forInOrOfStatement.awaitModifier,
42793
42807
Diagnostics.for_await_loops_are_only_allowed_at_the_top_level_of_a_file_when_that_file_is_a_module_but_this_file_has_no_imports_or_exports_Consider_adding_an_empty_export_to_make_this_file_a_module));
42794
42808
}
42795
- if ((moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System && !(moduleKind === ModuleKind.NodeNext && getSourceFileOfNode(forInOrOfStatement).impliedNodeFormat === ModuleKind.ESNext)) || languageVersion < ScriptTarget.ES2017) {
42809
+ if ((moduleKind !== ModuleKind.ES2022 && moduleKind !== ModuleKind. ESNext && moduleKind !== ModuleKind.System && !(moduleKind === ModuleKind.NodeNext && getSourceFileOfNode(forInOrOfStatement).impliedNodeFormat === ModuleKind.ESNext)) || languageVersion < ScriptTarget.ES2017) {
42796
42810
diagnostics.add(createDiagnosticForNode(forInOrOfStatement.awaitModifier,
42797
- Diagnostics.Top_level_for_await_loops_are_only_allowed_when_the_module_option_is_set_to_esnext_system_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher ));
42811
+ Diagnostics.Top_level_for_await_loops_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher ));
42798
42812
}
42799
42813
}
42800
42814
}
@@ -43558,7 +43572,7 @@ namespace ts {
43558
43572
43559
43573
function checkGrammarImportCallExpression(node: ImportCall): boolean {
43560
43574
if (moduleKind === ModuleKind.ES2015) {
43561
- return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_are_only_supported_when_the_module_flag_is_set_to_es2020_esnext_commonjs_amd_system_umd_node12_or_nodenext );
43575
+ return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_are_only_supported_when_the_module_flag_is_set_to_es2020_es2022_esnext_commonjs_amd_system_umd_node12_or_nodenext );
43562
43576
}
43563
43577
43564
43578
if (node.typeArguments) {
0 commit comments