@@ -4915,9 +4915,7 @@ namespace ts {
49154915 }
49164916 const widened = getWidenedType(addOptionality(type, definedInMethod && !definedInConstructor));
49174917 if (filterType(widened, t => !!(t.flags & ~TypeFlags.Nullable)) === neverType) {
4918- if (noImplicitAny) {
4919- reportImplicitAnyError(symbol.valueDeclaration, anyType);
4920- }
4918+ reportImplicitAny(symbol.valueDeclaration, anyType);
49214919 return anyType;
49224920 }
49234921 return widened;
@@ -4992,9 +4990,7 @@ namespace ts {
49924990 return result;
49934991 }
49944992 if (isEmptyArrayLiteralType(type)) {
4995- if (noImplicitAny) {
4996- reportImplicitAnyError(expression, anyArrayType);
4997- }
4993+ reportImplicitAny(expression, anyArrayType);
49984994 return anyArrayType;
49994995 }
50004996 return type;
@@ -5044,8 +5040,8 @@ namespace ts {
50445040 if (isBindingPattern(element.name)) {
50455041 return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors);
50465042 }
5047- if (reportErrors && noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) {
5048- reportImplicitAnyError (element, anyType);
5043+ if (reportErrors && !declarationBelongsToPrivateAmbientMember(element)) {
5044+ reportImplicitAny (element, anyType);
50495045 }
50505046 return anyType;
50515047 }
@@ -5145,9 +5141,9 @@ namespace ts {
51455141 type = isParameter(declaration) && declaration.dotDotDotToken ? anyArrayType : anyType;
51465142
51475143 // Report implicit any errors unless this is a private property within an ambient declaration
5148- if (reportErrors && noImplicitAny ) {
5144+ if (reportErrors) {
51495145 if (!declarationBelongsToPrivateAmbientMember(declaration)) {
5150- reportImplicitAnyError (declaration, type);
5146+ reportImplicitAny (declaration, type);
51515147 }
51525148 }
51535149 return type;
@@ -5328,14 +5324,12 @@ namespace ts {
53285324 }
53295325 // Otherwise, fall back to 'any'.
53305326 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));
53395333 }
53405334 type = anyType;
53415335 }
@@ -13282,8 +13276,12 @@ namespace ts {
1328213276 return errorReported;
1328313277 }
1328413278
13285- function reportImplicitAnyError (declaration: Declaration, type: Type) {
13279+ function reportImplicitAny (declaration: Declaration, type: Type) {
1328613280 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+ }
1328713285 let diagnostic: DiagnosticMessage;
1328813286 switch (declaration.kind) {
1328913287 case SyntaxKind.BinaryExpression:
@@ -13306,26 +13304,28 @@ namespace ts {
1330613304 case SyntaxKind.SetAccessor:
1330713305 case SyntaxKind.FunctionExpression:
1330813306 case SyntaxKind.ArrowFunction:
13309- if (!(declaration as NamedDeclaration).name) {
13307+ if (noImplicitAny && !(declaration as NamedDeclaration).name) {
1331013308 error(declaration, Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString);
1331113309 return;
1331213310 }
1331313311 diagnostic = Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type;
1331413312 break;
1331513313 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+ }
1331713317 return;
1331813318 default:
1331913319 diagnostic = Diagnostics.Variable_0_implicitly_has_an_1_type;
1332013320 }
13321- error( declaration, diagnostic, declarationNameToString(getNameOfDeclaration(declaration)), typeAsString);
13321+ errorOrSuggestion(noImplicitAny, declaration, diagnostic, declarationNameToString(getNameOfDeclaration(declaration)), typeAsString);
1332213322 }
1332313323
1332413324 function reportErrorsFromWidening(declaration: Declaration, type: Type) {
1332513325 if (produceDiagnostics && noImplicitAny && type.flags & TypeFlags.ContainsWideningType) {
1332613326 // Report implicit any error within type if possible, otherwise report error on declaration
1332713327 if (!reportWideningErrorsInType(type)) {
13328- reportImplicitAnyError (declaration, type);
13328+ reportImplicitAny (declaration, type);
1332913329 }
1333013330 }
1333113331 }
@@ -22319,15 +22319,11 @@ namespace ts {
2231922319 isTypeAssertion(initializer) ? type : getWidenedLiteralType(type);
2232022320 if (isInJSFile(declaration)) {
2232122321 if (widened.flags & TypeFlags.Nullable) {
22322- if (noImplicitAny) {
22323- reportImplicitAnyError(declaration, anyType);
22324- }
22322+ reportImplicitAny(declaration, anyType);
2232522323 return anyType;
2232622324 }
2232722325 else if (isEmptyArrayLiteralType(widened)) {
22328- if (noImplicitAny) {
22329- reportImplicitAnyError(declaration, anyArrayType);
22330- }
22326+ reportImplicitAny(declaration, anyArrayType);
2233122327 return anyArrayType;
2233222328 }
2233322329 }
@@ -23318,8 +23314,8 @@ namespace ts {
2331823314 checkSourceElement(node.typeParameter);
2331923315 checkSourceElement(node.type);
2332023316
23321- if (noImplicitAny && !node.type) {
23322- reportImplicitAnyError (node, anyType);
23317+ if (!node.type) {
23318+ reportImplicitAny (node, anyType);
2332323319 }
2332423320
2332523321 const type = <MappedType>getTypeFromMappedTypeNode(node);
@@ -24343,8 +24339,8 @@ namespace ts {
2434324339 if (produceDiagnostics && !getEffectiveReturnTypeNode(node)) {
2434424340 // Report an implicit any error if there is no body, no explicit return type, and node is not a private method
2434524341 // 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);
2434824344 }
2434924345
2435024346 if (functionFlags & FunctionFlags.Generator && nodeIsPresent(body)) {
0 commit comments