@@ -94,8 +94,6 @@ namespace ts {
94
94
95
95
const globalThisSymbol = createSymbol(SymbolFlags.Module, "globalThis" as __String, CheckFlags.Readonly);
96
96
globalThisSymbol.exports = globals;
97
- globalThisSymbol.valueDeclaration = createNode(SyntaxKind.Identifier) as Identifier;
98
- (globalThisSymbol.valueDeclaration as Identifier).escapedText = "globalThis" as __String;
99
97
globals.set(globalThisSymbol.escapedName, globalThisSymbol);
100
98
101
99
const argumentsSymbol = createSymbol(SymbolFlags.Property, "arguments" as __String);
@@ -926,7 +924,12 @@ namespace ts {
926
924
recordMergedSymbol(target, source);
927
925
}
928
926
else if (target.flags & SymbolFlags.NamespaceModule) {
929
- error(getNameOfDeclaration(source.declarations[0]), Diagnostics.Cannot_augment_module_0_with_value_exports_because_it_resolves_to_a_non_module_entity, symbolToString(target));
927
+ // Do not report an error when merging `var globalThis` with the built-in `globalThis`,
928
+ // as we will already report a "Declaration name conflicts..." error, and this error
929
+ // won't make much sense.
930
+ if (target !== globalThisSymbol) {
931
+ error(getNameOfDeclaration(source.declarations[0]), Diagnostics.Cannot_augment_module_0_with_value_exports_because_it_resolves_to_a_non_module_entity, symbolToString(target));
932
+ }
930
933
}
931
934
else { // error
932
935
const isEitherEnum = !!(target.flags & SymbolFlags.Enum || source.flags & SymbolFlags.Enum);
@@ -1409,15 +1412,14 @@ namespace ts {
1409
1412
}
1410
1413
break;
1411
1414
case SyntaxKind.PropertyDeclaration:
1412
- case SyntaxKind.PropertySignature:
1413
1415
// TypeScript 1.0 spec (April 2014): 8.4.1
1414
1416
// Initializer expressions for instance member variables are evaluated in the scope
1415
1417
// of the class constructor body but are not permitted to reference parameters or
1416
1418
// local variables of the constructor. This effectively means that entities from outer scopes
1417
1419
// by the same name as a constructor parameter or local variable are inaccessible
1418
1420
// in initializer expressions for instance member variables.
1419
- if (isClassLike(location.parent) && !hasModifier(location, ModifierFlags.Static)) {
1420
- const ctor = findConstructorDeclaration(location.parent);
1421
+ if (!hasModifier(location, ModifierFlags.Static)) {
1422
+ const ctor = findConstructorDeclaration(location.parent as ClassLikeDeclaration );
1421
1423
if (ctor && ctor.locals) {
1422
1424
if (lookup(ctor.locals, name, meaning & SymbolFlags.Value)) {
1423
1425
// Remember the property node, it will be used later to report appropriate error
@@ -9943,13 +9945,13 @@ namespace ts {
9943
9945
return type.flags & TypeFlags.Union ? getIntersectionType(map((<IntersectionType>type).types, t => getIndexType(t, stringsOnly, noIndexSignatures))) :
9944
9946
type.flags & TypeFlags.Intersection ? getUnionType(map((<IntersectionType>type).types, t => getIndexType(t, stringsOnly, noIndexSignatures))) :
9945
9947
maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive) ? getIndexTypeForGenericType(<InstantiableType | UnionOrIntersectionType>type, stringsOnly) :
9946
- getObjectFlags(type) & ObjectFlags.Mapped ? filterType(getConstraintTypeFromMappedType(<MappedType>type), t => !(noIndexSignatures && t.flags & (TypeFlags.Any | TypeFlags.String | TypeFlags.Number ))) :
9948
+ getObjectFlags(type) & ObjectFlags.Mapped ? filterType(getConstraintTypeFromMappedType(<MappedType>type), t => !(noIndexSignatures && t.flags & (TypeFlags.Any | TypeFlags.String))) :
9947
9949
type === wildcardType ? wildcardType :
9948
9950
type.flags & TypeFlags.Unknown ? neverType :
9949
9951
type.flags & (TypeFlags.Any | TypeFlags.Never) ? keyofConstraintType :
9950
9952
stringsOnly ? !noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromProperties(type, TypeFlags.StringLiteral) :
9951
9953
!noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? getUnionType([stringType, numberType, getLiteralTypeFromProperties(type, TypeFlags.UniqueESSymbol)]) :
9952
- !noIndexSignatures && getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromProperties(type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol)]) :
9954
+ getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromProperties(type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol)]) :
9953
9955
getLiteralTypeFromProperties(type, TypeFlags.StringOrNumberLiteralOrUnique);
9954
9956
}
9955
9957
@@ -10067,10 +10069,10 @@ namespace ts {
10067
10069
if (objectType.flags & (TypeFlags.Any | TypeFlags.Never)) {
10068
10070
return objectType;
10069
10071
}
10070
- const indexInfo = isTypeAssignableToKind(indexType, TypeFlags.NumberLike) && getIndexInfoOfType(objectType, IndexKind.Number) ||
10071
- getIndexInfoOfType(objectType, IndexKind.String) ;
10072
+ const stringIndexInfo = getIndexInfoOfType(objectType, IndexKind.String);
10073
+ const indexInfo = isTypeAssignableToKind(indexType, TypeFlags.NumberLike) && getIndexInfoOfType(objectType, IndexKind.Number) || stringIndexInfo ;
10072
10074
if (indexInfo) {
10073
- if (accessFlags & AccessFlags.NoIndexSignatures) {
10075
+ if (accessFlags & AccessFlags.NoIndexSignatures && indexInfo === stringIndexInfo ) {
10074
10076
if (accessExpression) {
10075
10077
error(accessExpression, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(originalObjectType));
10076
10078
}
@@ -14228,6 +14230,32 @@ namespace ts {
14228
14230
return strictNullChecks ? getGlobalNonNullableTypeInstantiation(type) : type;
14229
14231
}
14230
14232
14233
+
14234
+ /**
14235
+ * Is source potentially coercible to target type under `==`.
14236
+ * Assumes that `source` is a constituent of a union, hence
14237
+ * the boolean literal flag on the LHS, but not on the RHS.
14238
+ *
14239
+ * This does not fully replicate the semantics of `==`. The
14240
+ * intention is to catch cases that are clearly not right.
14241
+ *
14242
+ * Comparing (string | number) to number should not remove the
14243
+ * string element.
14244
+ *
14245
+ * Comparing (string | number) to 1 will remove the string
14246
+ * element, though this is not sound. This is a pragmatic
14247
+ * choice.
14248
+ *
14249
+ * @see narrowTypeByEquality
14250
+ *
14251
+ * @param source
14252
+ * @param target
14253
+ */
14254
+ function isCoercibleUnderDoubleEquals(source: Type, target: Type): boolean {
14255
+ return ((source.flags & (TypeFlags.Number | TypeFlags.String | TypeFlags.BooleanLiteral)) !== 0)
14256
+ && ((target.flags & (TypeFlags.Number | TypeFlags.String | TypeFlags.Boolean)) !== 0);
14257
+ }
14258
+
14231
14259
/**
14232
14260
* Return true if type was inferred from an object literal, written as an object type literal, or is the shape of a module
14233
14261
* with no call or construct signatures.
@@ -16571,7 +16599,10 @@ namespace ts {
16571
16599
return type;
16572
16600
}
16573
16601
if (assumeTrue) {
16574
- const narrowedType = filterType(type, t => areTypesComparable(t, valueType));
16602
+ const filterFn: (t: Type) => boolean = operator === SyntaxKind.EqualsEqualsToken ?
16603
+ (t => areTypesComparable(t, valueType) || isCoercibleUnderDoubleEquals(t, valueType)) :
16604
+ t => areTypesComparable(t, valueType);
16605
+ const narrowedType = filterType(type, filterFn);
16575
16606
return narrowedType.flags & TypeFlags.Never ? type : replacePrimitivesWithLiterals(narrowedType, valueType);
16576
16607
}
16577
16608
if (isUnitType(valueType)) {
@@ -21973,6 +22004,13 @@ namespace ts {
21973
22004
const arg = (<PrefixUnaryExpression>node).operand;
21974
22005
return op === SyntaxKind.MinusToken && (arg.kind === SyntaxKind.NumericLiteral || arg.kind === SyntaxKind.BigIntLiteral) ||
21975
22006
op === SyntaxKind.PlusToken && arg.kind === SyntaxKind.NumericLiteral;
22007
+ case SyntaxKind.PropertyAccessExpression:
22008
+ case SyntaxKind.ElementAccessExpression:
22009
+ const expr = (<PropertyAccessExpression | ElementAccessExpression>node).expression;
22010
+ if (isIdentifier(expr)) {
22011
+ const symbol = getSymbolAtLocation(expr);
22012
+ return !!(symbol && (symbol.flags & SymbolFlags.Enum) && getEnumKind(symbol) === EnumKind.Literal);
22013
+ }
21976
22014
}
21977
22015
return false;
21978
22016
}
@@ -21981,7 +22019,7 @@ namespace ts {
21981
22019
let exprType = checkExpression(expression, checkMode);
21982
22020
if (isConstTypeReference(type)) {
21983
22021
if (!isValidConstAssertionArgument(expression)) {
21984
- error(expression, Diagnostics.A_const_assertion_can_only_be_applied_to_a_string_number_boolean_array_or_object_literal );
22022
+ error(expression, Diagnostics.A_const_assertions_can_only_be_applied_to_references_to_enum_members_or_string_number_boolean_array_or_object_literals );
21985
22023
}
21986
22024
return getRegularTypeOfLiteralType(exprType);
21987
22025
}
@@ -25629,7 +25667,7 @@ namespace ts {
25629
25667
}
25630
25668
25631
25669
if (!compilerOptions.experimentalDecorators) {
25632
- error(node, Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_to_remove_this_warning );
25670
+ error(node, Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_in_your_tsconfig_or_jsconfig_to_remove_this_warning );
25633
25671
}
25634
25672
25635
25673
const firstDecorator = node.decorators[0];
@@ -26427,14 +26465,28 @@ namespace ts {
26427
26465
}
26428
26466
// For a binding pattern, validate the initializer and exit
26429
26467
if (isBindingPattern(node.name)) {
26430
- // Don't validate for-in initializer as it is already an error
26431
- if (node.initializer && node.parent.parent.kind !== SyntaxKind.ForInStatement) {
26432
- const initializerType = checkExpressionCached(node.initializer);
26433
- if (strictNullChecks && node.name.elements.length === 0) {
26434
- checkNonNullNonVoidType(initializerType, node);
26468
+ const needCheckInitializer = node.initializer && node.parent.parent.kind !== SyntaxKind.ForInStatement;
26469
+ const needCheckWidenedType = node.name.elements.length === 0;
26470
+ if (needCheckInitializer || needCheckWidenedType) {
26471
+ // Don't validate for-in initializer as it is already an error
26472
+ const widenedType = getWidenedTypeForVariableLikeDeclaration(node);
26473
+ if (needCheckInitializer) {
26474
+ const initializerType = checkExpressionCached(node.initializer!);
26475
+ if (strictNullChecks && needCheckWidenedType) {
26476
+ checkNonNullNonVoidType(initializerType, node);
26477
+ }
26478
+ else {
26479
+ checkTypeAssignableToAndOptionallyElaborate(initializerType, getWidenedTypeForVariableLikeDeclaration(node), node, node.initializer);
26480
+ }
26435
26481
}
26436
- else {
26437
- checkTypeAssignableToAndOptionallyElaborate(initializerType, getWidenedTypeForVariableLikeDeclaration(node), node, node.initializer);
26482
+ // check the binding pattern with empty elements
26483
+ if (needCheckWidenedType) {
26484
+ if (isArrayBindingPattern(node.name)) {
26485
+ checkIteratedTypeOrElementType(widenedType, node, /* allowStringInput */ false, /* allowAsyncIterables */ false);
26486
+ }
26487
+ else if (strictNullChecks) {
26488
+ checkNonNullNonVoidType(widenedType, node);
26489
+ }
26438
26490
}
26439
26491
}
26440
26492
return;
@@ -30407,6 +30459,14 @@ namespace ts {
30407
30459
continue;
30408
30460
}
30409
30461
if (!isExternalOrCommonJsModule(file)) {
30462
+ // It is an error for a non-external-module (i.e. script) to declare its own `globalThis`.
30463
+ // We can't use `builtinGlobals` for this due to synthetic expando-namespace generation in JS files.
30464
+ const fileGlobalThisSymbol = file.locals!.get("globalThis" as __String);
30465
+ if (fileGlobalThisSymbol) {
30466
+ for (const declaration of fileGlobalThisSymbol.declarations) {
30467
+ diagnostics.add(createDiagnosticForNode(declaration, Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0, "globalThis"));
30468
+ }
30469
+ }
30410
30470
mergeSymbolTable(globals, file.locals!);
30411
30471
}
30412
30472
if (file.jsGlobalAugmentations) {
@@ -30452,6 +30512,7 @@ namespace ts {
30452
30512
getSymbolLinks(undefinedSymbol).type = undefinedWideningType;
30453
30513
getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments" as __String, /*arity*/ 0, /*reportErrors*/ true);
30454
30514
getSymbolLinks(unknownSymbol).type = errorType;
30515
+ getSymbolLinks(globalThisSymbol).type = createObjectType(ObjectFlags.Anonymous, globalThisSymbol);
30455
30516
30456
30517
// Initialize special types
30457
30518
globalArrayType = getGlobalType("Array" as __String, /*arity*/ 1, /*reportErrors*/ true);
@@ -31179,6 +31240,13 @@ namespace ts {
31179
31240
31180
31241
for (const prop of node.properties) {
31181
31242
if (prop.kind === SyntaxKind.SpreadAssignment) {
31243
+ if (inDestructuring) {
31244
+ // a rest property cannot be destructured any further
31245
+ const expression = skipParentheses(prop.expression);
31246
+ if (isArrayLiteralExpression(expression) || isObjectLiteralExpression(expression)) {
31247
+ return grammarErrorOnNode(prop.expression, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern);
31248
+ }
31249
+ }
31182
31250
continue;
31183
31251
}
31184
31252
const name = prop.name;
@@ -31841,7 +31909,7 @@ namespace ts {
31841
31909
return false;
31842
31910
}
31843
31911
31844
- return grammarErrorOnFirstToken(node, Diagnostics.A_declare_modifier_is_required_for_a_top_level_declaration_in_a_d_ts_file );
31912
+ return grammarErrorOnFirstToken(node, Diagnostics.Top_level_declarations_in_d_ts_files_must_start_with_either_a_declare_or_export_modifier );
31845
31913
}
31846
31914
31847
31915
function checkGrammarTopLevelElementsForRequiredDeclareModifier(file: SourceFile): boolean {
0 commit comments