@@ -1410,15 +1410,14 @@ namespace ts {
1410
1410
}
1411
1411
break;
1412
1412
case SyntaxKind.PropertyDeclaration:
1413
- case SyntaxKind.PropertySignature:
1414
1413
// TypeScript 1.0 spec (April 2014): 8.4.1
1415
1414
// Initializer expressions for instance member variables are evaluated in the scope
1416
1415
// of the class constructor body but are not permitted to reference parameters or
1417
1416
// local variables of the constructor. This effectively means that entities from outer scopes
1418
1417
// by the same name as a constructor parameter or local variable are inaccessible
1419
1418
// in initializer expressions for instance member variables.
1420
- if (isClassLike(location.parent) && !hasModifier(location, ModifierFlags.Static)) {
1421
- const ctor = findConstructorDeclaration(location.parent);
1419
+ if (!hasModifier(location, ModifierFlags.Static)) {
1420
+ const ctor = findConstructorDeclaration(location.parent as ClassLikeDeclaration );
1422
1421
if (ctor && ctor.locals) {
1423
1422
if (lookup(ctor.locals, name, meaning & SymbolFlags.Value)) {
1424
1423
// Remember the property node, it will be used later to report appropriate error
@@ -9944,13 +9943,13 @@ namespace ts {
9944
9943
return type.flags & TypeFlags.Union ? getIntersectionType(map((<IntersectionType>type).types, t => getIndexType(t, stringsOnly, noIndexSignatures))) :
9945
9944
type.flags & TypeFlags.Intersection ? getUnionType(map((<IntersectionType>type).types, t => getIndexType(t, stringsOnly, noIndexSignatures))) :
9946
9945
maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive) ? getIndexTypeForGenericType(<InstantiableType | UnionOrIntersectionType>type, stringsOnly) :
9947
- getObjectFlags(type) & ObjectFlags.Mapped ? filterType(getConstraintTypeFromMappedType(<MappedType>type), t => !(noIndexSignatures && t.flags & (TypeFlags.Any | TypeFlags.String | TypeFlags.Number ))) :
9946
+ getObjectFlags(type) & ObjectFlags.Mapped ? filterType(getConstraintTypeFromMappedType(<MappedType>type), t => !(noIndexSignatures && t.flags & (TypeFlags.Any | TypeFlags.String))) :
9948
9947
type === wildcardType ? wildcardType :
9949
9948
type.flags & TypeFlags.Unknown ? neverType :
9950
9949
type.flags & (TypeFlags.Any | TypeFlags.Never) ? keyofConstraintType :
9951
9950
stringsOnly ? !noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromProperties(type, TypeFlags.StringLiteral) :
9952
9951
!noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? getUnionType([stringType, numberType, getLiteralTypeFromProperties(type, TypeFlags.UniqueESSymbol)]) :
9953
- !noIndexSignatures && getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromProperties(type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol)]) :
9952
+ getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromProperties(type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol)]) :
9954
9953
getLiteralTypeFromProperties(type, TypeFlags.StringOrNumberLiteralOrUnique);
9955
9954
}
9956
9955
@@ -10068,10 +10067,10 @@ namespace ts {
10068
10067
if (objectType.flags & (TypeFlags.Any | TypeFlags.Never)) {
10069
10068
return objectType;
10070
10069
}
10071
- const indexInfo = isTypeAssignableToKind(indexType, TypeFlags.NumberLike) && getIndexInfoOfType(objectType, IndexKind.Number) ||
10072
- getIndexInfoOfType(objectType, IndexKind.String) ;
10070
+ const stringIndexInfo = getIndexInfoOfType(objectType, IndexKind.String);
10071
+ const indexInfo = isTypeAssignableToKind(indexType, TypeFlags.NumberLike) && getIndexInfoOfType(objectType, IndexKind.Number) || stringIndexInfo ;
10073
10072
if (indexInfo) {
10074
- if (accessFlags & AccessFlags.NoIndexSignatures) {
10073
+ if (accessFlags & AccessFlags.NoIndexSignatures && indexInfo === stringIndexInfo ) {
10075
10074
if (accessExpression) {
10076
10075
error(accessExpression, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(originalObjectType));
10077
10076
}
@@ -14270,6 +14269,32 @@ namespace ts {
14270
14269
return strictNullChecks ? getGlobalNonNullableTypeInstantiation(type) : type;
14271
14270
}
14272
14271
14272
+
14273
+ /**
14274
+ * Is source potentially coercible to target type under `==`.
14275
+ * Assumes that `source` is a constituent of a union, hence
14276
+ * the boolean literal flag on the LHS, but not on the RHS.
14277
+ *
14278
+ * This does not fully replicate the semantics of `==`. The
14279
+ * intention is to catch cases that are clearly not right.
14280
+ *
14281
+ * Comparing (string | number) to number should not remove the
14282
+ * string element.
14283
+ *
14284
+ * Comparing (string | number) to 1 will remove the string
14285
+ * element, though this is not sound. This is a pragmatic
14286
+ * choice.
14287
+ *
14288
+ * @see narrowTypeByEquality
14289
+ *
14290
+ * @param source
14291
+ * @param target
14292
+ */
14293
+ function isCoercibleUnderDoubleEquals(source: Type, target: Type): boolean {
14294
+ return ((source.flags & (TypeFlags.Number | TypeFlags.String | TypeFlags.BooleanLiteral)) !== 0)
14295
+ && ((target.flags & (TypeFlags.Number | TypeFlags.String | TypeFlags.Boolean)) !== 0);
14296
+ }
14297
+
14273
14298
/**
14274
14299
* Return true if type was inferred from an object literal, written as an object type literal, or is the shape of a module
14275
14300
* with no call or construct signatures.
@@ -16613,7 +16638,10 @@ namespace ts {
16613
16638
return type;
16614
16639
}
16615
16640
if (assumeTrue) {
16616
- const narrowedType = filterType(type, t => areTypesComparable(t, valueType));
16641
+ const filterFn: (t: Type) => boolean = operator === SyntaxKind.EqualsEqualsToken ?
16642
+ (t => areTypesComparable(t, valueType) || isCoercibleUnderDoubleEquals(t, valueType)) :
16643
+ t => areTypesComparable(t, valueType);
16644
+ const narrowedType = filterType(type, filterFn);
16617
16645
return narrowedType.flags & TypeFlags.Never ? type : replacePrimitivesWithLiterals(narrowedType, valueType);
16618
16646
}
16619
16647
if (isUnitType(valueType)) {
@@ -25678,7 +25706,7 @@ namespace ts {
25678
25706
}
25679
25707
25680
25708
if (!compilerOptions.experimentalDecorators) {
25681
- 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 );
25709
+ 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 );
25682
25710
}
25683
25711
25684
25712
const firstDecorator = node.decorators[0];
@@ -26476,14 +26504,28 @@ namespace ts {
26476
26504
}
26477
26505
// For a binding pattern, validate the initializer and exit
26478
26506
if (isBindingPattern(node.name)) {
26479
- // Don't validate for-in initializer as it is already an error
26480
- if (node.initializer && node.parent.parent.kind !== SyntaxKind.ForInStatement) {
26481
- const initializerType = checkExpressionCached(node.initializer);
26482
- if (strictNullChecks && node.name.elements.length === 0) {
26483
- checkNonNullNonVoidType(initializerType, node);
26507
+ const needCheckInitializer = node.initializer && node.parent.parent.kind !== SyntaxKind.ForInStatement;
26508
+ const needCheckWidenedType = node.name.elements.length === 0;
26509
+ if (needCheckInitializer || needCheckWidenedType) {
26510
+ // Don't validate for-in initializer as it is already an error
26511
+ const widenedType = getWidenedTypeForVariableLikeDeclaration(node);
26512
+ if (needCheckInitializer) {
26513
+ const initializerType = checkExpressionCached(node.initializer!);
26514
+ if (strictNullChecks && needCheckWidenedType) {
26515
+ checkNonNullNonVoidType(initializerType, node);
26516
+ }
26517
+ else {
26518
+ checkTypeAssignableToAndOptionallyElaborate(initializerType, getWidenedTypeForVariableLikeDeclaration(node), node, node.initializer);
26519
+ }
26484
26520
}
26485
- else {
26486
- checkTypeAssignableToAndOptionallyElaborate(initializerType, getWidenedTypeForVariableLikeDeclaration(node), node, node.initializer);
26521
+ // check the binding pattern with empty elements
26522
+ if (needCheckWidenedType) {
26523
+ if (isArrayBindingPattern(node.name)) {
26524
+ checkIteratedTypeOrElementType(widenedType, node, /* allowStringInput */ false, /* allowAsyncIterables */ false);
26525
+ }
26526
+ else if (strictNullChecks) {
26527
+ checkNonNullNonVoidType(widenedType, node);
26528
+ }
26487
26529
}
26488
26530
}
26489
26531
return;
@@ -31228,6 +31270,13 @@ namespace ts {
31228
31270
31229
31271
for (const prop of node.properties) {
31230
31272
if (prop.kind === SyntaxKind.SpreadAssignment) {
31273
+ if (inDestructuring) {
31274
+ // a rest property cannot be destructured any further
31275
+ const expression = skipParentheses(prop.expression);
31276
+ if (isArrayLiteralExpression(expression) || isObjectLiteralExpression(expression)) {
31277
+ return grammarErrorOnNode(prop.expression, Diagnostics.A_rest_element_cannot_contain_a_binding_pattern);
31278
+ }
31279
+ }
31231
31280
continue;
31232
31281
}
31233
31282
const name = prop.name;
@@ -31890,7 +31939,7 @@ namespace ts {
31890
31939
return false;
31891
31940
}
31892
31941
31893
- return grammarErrorOnFirstToken(node, Diagnostics.A_declare_modifier_is_required_for_a_top_level_declaration_in_a_d_ts_file );
31942
+ return grammarErrorOnFirstToken(node, Diagnostics.Top_level_declarations_in_d_ts_files_must_start_with_either_a_declare_or_export_modifier );
31894
31943
}
31895
31944
31896
31945
function checkGrammarTopLevelElementsForRequiredDeclareModifier(file: SourceFile): boolean {
0 commit comments