Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/rules/no-inferrable-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export default createRule<Options, MessageIds>({
*/
function reportInferrableType(
node:
| TSESTree.AccessorProperty
| TSESTree.Parameter
| TSESTree.PropertyDefinition
| TSESTree.VariableDeclarator,
Expand Down Expand Up @@ -265,7 +266,7 @@ export default createRule<Options, MessageIds>({
}

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
Expand All @@ -278,6 +279,7 @@ export default createRule<Options, MessageIds>({
}

return {
AccessorProperty: inferrablePropertyVisitor,
ArrowFunctionExpression: inferrableParameterVisitor,
FunctionDeclaration: inferrableParameterVisitor,
FunctionExpression: inferrableParameterVisitor,
Expand Down
35 changes: 35 additions & 0 deletions packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {};",
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
`,
},
Expand Down
Loading