@@ -4915,9 +4915,7 @@ namespace ts {
4915
4915
}
4916
4916
const widened = getWidenedType(addOptionality(type, definedInMethod && !definedInConstructor));
4917
4917
if (filterType(widened, t => !!(t.flags & ~TypeFlags.Nullable)) === neverType) {
4918
- if (noImplicitAny) {
4919
- reportImplicitAnyError(symbol.valueDeclaration, anyType);
4920
- }
4918
+ reportImplicitAny(symbol.valueDeclaration, anyType);
4921
4919
return anyType;
4922
4920
}
4923
4921
return widened;
@@ -4992,9 +4990,7 @@ namespace ts {
4992
4990
return result;
4993
4991
}
4994
4992
if (isEmptyArrayLiteralType(type)) {
4995
- if (noImplicitAny) {
4996
- reportImplicitAnyError(expression, anyArrayType);
4997
- }
4993
+ reportImplicitAny(expression, anyArrayType);
4998
4994
return anyArrayType;
4999
4995
}
5000
4996
return type;
@@ -5044,8 +5040,8 @@ namespace ts {
5044
5040
if (isBindingPattern(element.name)) {
5045
5041
return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors);
5046
5042
}
5047
- if (reportErrors && noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) {
5048
- reportImplicitAnyError (element, anyType);
5043
+ if (reportErrors && !declarationBelongsToPrivateAmbientMember(element)) {
5044
+ reportImplicitAny (element, anyType);
5049
5045
}
5050
5046
return anyType;
5051
5047
}
@@ -5145,9 +5141,9 @@ namespace ts {
5145
5141
type = isParameter(declaration) && declaration.dotDotDotToken ? anyArrayType : anyType;
5146
5142
5147
5143
// Report implicit any errors unless this is a private property within an ambient declaration
5148
- if (reportErrors && noImplicitAny ) {
5144
+ if (reportErrors) {
5149
5145
if (!declarationBelongsToPrivateAmbientMember(declaration)) {
5150
- reportImplicitAnyError (declaration, type);
5146
+ reportImplicitAny (declaration, type);
5151
5147
}
5152
5148
}
5153
5149
return type;
@@ -5328,14 +5324,12 @@ namespace ts {
5328
5324
}
5329
5325
// Otherwise, fall back to 'any'.
5330
5326
else {
5331
- if (noImplicitAny) {
5332
- if (setter) {
5333
- error(setter, Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol));
5334
- }
5335
- else {
5336
- Debug.assert(!!getter, "there must existed getter as we are current checking either setter or getter in this function");
5337
- error(getter, Diagnostics.Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation, symbolToString(symbol));
5338
- }
5327
+ if (setter) {
5328
+ errorOrSuggestion(noImplicitAny, setter, Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol));
5329
+ }
5330
+ else {
5331
+ Debug.assert(!!getter, "there must existed getter as we are current checking either setter or getter in this function");
5332
+ errorOrSuggestion(noImplicitAny, getter!, Diagnostics.Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation, symbolToString(symbol));
5339
5333
}
5340
5334
type = anyType;
5341
5335
}
@@ -13282,8 +13276,12 @@ namespace ts {
13282
13276
return errorReported;
13283
13277
}
13284
13278
13285
- function reportImplicitAnyError (declaration: Declaration, type: Type) {
13279
+ function reportImplicitAny (declaration: Declaration, type: Type) {
13286
13280
const typeAsString = typeToString(getWidenedType(type));
13281
+ if (isInJSFile(declaration) && !isCheckJsEnabledForFile(getSourceFileOfNode(declaration), compilerOptions)) {
13282
+ // Only report implicit any errors/suggestions in TS and ts-check JS files
13283
+ return;
13284
+ }
13287
13285
let diagnostic: DiagnosticMessage;
13288
13286
switch (declaration.kind) {
13289
13287
case SyntaxKind.BinaryExpression:
@@ -13306,26 +13304,28 @@ namespace ts {
13306
13304
case SyntaxKind.SetAccessor:
13307
13305
case SyntaxKind.FunctionExpression:
13308
13306
case SyntaxKind.ArrowFunction:
13309
- if (!(declaration as NamedDeclaration).name) {
13307
+ if (noImplicitAny && !(declaration as NamedDeclaration).name) {
13310
13308
error(declaration, Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString);
13311
13309
return;
13312
13310
}
13313
13311
diagnostic = Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type;
13314
13312
break;
13315
13313
case SyntaxKind.MappedType:
13316
- error(declaration, Diagnostics.Mapped_object_type_implicitly_has_an_any_template_type);
13314
+ if (noImplicitAny) {
13315
+ error(declaration, Diagnostics.Mapped_object_type_implicitly_has_an_any_template_type);
13316
+ }
13317
13317
return;
13318
13318
default:
13319
13319
diagnostic = Diagnostics.Variable_0_implicitly_has_an_1_type;
13320
13320
}
13321
- error( declaration, diagnostic, declarationNameToString(getNameOfDeclaration(declaration)), typeAsString);
13321
+ errorOrSuggestion(noImplicitAny, declaration, diagnostic, declarationNameToString(getNameOfDeclaration(declaration)), typeAsString);
13322
13322
}
13323
13323
13324
13324
function reportErrorsFromWidening(declaration: Declaration, type: Type) {
13325
13325
if (produceDiagnostics && noImplicitAny && type.flags & TypeFlags.ContainsWideningType) {
13326
13326
// Report implicit any error within type if possible, otherwise report error on declaration
13327
13327
if (!reportWideningErrorsInType(type)) {
13328
- reportImplicitAnyError (declaration, type);
13328
+ reportImplicitAny (declaration, type);
13329
13329
}
13330
13330
}
13331
13331
}
@@ -22319,15 +22319,11 @@ namespace ts {
22319
22319
isTypeAssertion(initializer) ? type : getWidenedLiteralType(type);
22320
22320
if (isInJSFile(declaration)) {
22321
22321
if (widened.flags & TypeFlags.Nullable) {
22322
- if (noImplicitAny) {
22323
- reportImplicitAnyError(declaration, anyType);
22324
- }
22322
+ reportImplicitAny(declaration, anyType);
22325
22323
return anyType;
22326
22324
}
22327
22325
else if (isEmptyArrayLiteralType(widened)) {
22328
- if (noImplicitAny) {
22329
- reportImplicitAnyError(declaration, anyArrayType);
22330
- }
22326
+ reportImplicitAny(declaration, anyArrayType);
22331
22327
return anyArrayType;
22332
22328
}
22333
22329
}
@@ -23318,8 +23314,8 @@ namespace ts {
23318
23314
checkSourceElement(node.typeParameter);
23319
23315
checkSourceElement(node.type);
23320
23316
23321
- if (noImplicitAny && !node.type) {
23322
- reportImplicitAnyError (node, anyType);
23317
+ if (!node.type) {
23318
+ reportImplicitAny (node, anyType);
23323
23319
}
23324
23320
23325
23321
const type = <MappedType>getTypeFromMappedTypeNode(node);
@@ -24343,8 +24339,8 @@ namespace ts {
24343
24339
if (produceDiagnostics && !getEffectiveReturnTypeNode(node)) {
24344
24340
// Report an implicit any error if there is no body, no explicit return type, and node is not a private method
24345
24341
// in an ambient context
24346
- if (noImplicitAny && nodeIsMissing(body) && !isPrivateWithinAmbient(node)) {
24347
- reportImplicitAnyError (node, anyType);
24342
+ if (nodeIsMissing(body) && !isPrivateWithinAmbient(node)) {
24343
+ reportImplicitAny (node, anyType);
24348
24344
}
24349
24345
24350
24346
if (functionFlags & FunctionFlags.Generator && nodeIsPresent(body)) {
0 commit comments