diff --git a/packages/eslint-plugin/src/rules/no-inferrable-types.ts b/packages/eslint-plugin/src/rules/no-inferrable-types.ts index 8c18f786e143..4d8496a7b288 100644 --- a/packages/eslint-plugin/src/rules/no-inferrable-types.ts +++ b/packages/eslint-plugin/src/rules/no-inferrable-types.ts @@ -193,6 +193,7 @@ export default createRule({ */ function reportInferrableType( node: + | TSESTree.AccessorProperty | TSESTree.Parameter | TSESTree.PropertyDefinition | TSESTree.VariableDeclarator, @@ -265,7 +266,7 @@ export default createRule({ } function inferrablePropertyVisitor( - node: TSESTree.PropertyDefinition, + node: TSESTree.AccessorProperty | TSESTree.PropertyDefinition, ): void { // We ignore `readonly` because of Microsoft/TypeScript#14416 // Essentially a readonly property without a type @@ -278,6 +279,7 @@ export default createRule({ } return { + AccessorProperty: inferrablePropertyVisitor, ArrowFunctionExpression: inferrableParameterVisitor, FunctionDeclaration: inferrableParameterVisitor, FunctionExpression: inferrableParameterVisitor, diff --git a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts index 13b71358e3e0..79304307ea3d 100644 --- a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts +++ b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts @@ -113,6 +113,11 @@ class Foo { readonly a: number = 5; } `, + ` +class Foo { + accessor a = 5; +} + `, 'const a: any = 5;', "const fn = function (a: any = 5, b: any = true, c: any = 'foo') {};", @@ -141,6 +146,14 @@ class Foo { }, { code: ` +class Foo { + accessor a: number = 5; +} + `, + options: [{ ignoreProperties: true }], + }, + { + code: ` class Foo { a?: number = 5; b?: boolean = true; @@ -316,6 +329,28 @@ class Foo { output: ` class Foo { constructor(public a = true) {} +} + `, + }, + { + code: ` +class Foo { + accessor a: number = 5; +} + `, + errors: [ + { + column: 3, + data: { + type: 'number', + }, + line: 3, + messageId: 'noInferrableType', + }, + ], + output: ` +class Foo { + accessor a = 5; } `, },