diff --git a/packages/eslint-plugin/src/rules/typedef.ts b/packages/eslint-plugin/src/rules/typedef.ts index 3d9d76f3872..19c4244d9f9 100644 --- a/packages/eslint-plugin/src/rules/typedef.ts +++ b/packages/eslint-plugin/src/rules/typedef.ts @@ -79,6 +79,15 @@ export default util.createRule<[Options], MessageIds>({ break; case AST_NODE_TYPES.TSParameterProperty: annotationNode = param.parameter; + + // Check TS parameter property with default value like `constructor(private param: string = 'something') {}` + if ( + annotationNode && + annotationNode.type === AST_NODE_TYPES.AssignmentPattern + ) { + annotationNode = annotationNode.left; + } + break; default: annotationNode = param; diff --git a/packages/eslint-plugin/tests/rules/typedef.test.ts b/packages/eslint-plugin/tests/rules/typedef.test.ts index d5ea514f59e..c9e35c356eb 100644 --- a/packages/eslint-plugin/tests/rules/typedef.test.ts +++ b/packages/eslint-plugin/tests/rules/typedef.test.ts @@ -109,6 +109,19 @@ ruleTester.run('typedef', rule, { `function receivesString({ a }: { a: string }): void { }`, `function receivesStrings({ a, b }: { [i: string ]: string }): void { }`, `function receivesNumber(a: number = 123): void { }`, + // Constructor parameters + `class Test { + constructor() {} + }`, + `class Test { + constructor(param: string) {} + }`, + `class Test { + constructor(param: string = 'something') {} + }`, + `class Test { + constructor(private param: string = 'something') {} + }`, // Method parameters `class Test { public method(x: number): number { return x; } @@ -401,6 +414,40 @@ ruleTester.run('typedef', rule, { }, ], }, + // Constructor parameters + { + code: `class Test { + constructor(param) {} + }`, + errors: [ + { + column: 21, + messageId: 'expectedTypedefNamed', + }, + ], + }, + { + code: `class Test { + constructor(param = 'something') {} + }`, + errors: [ + { + column: 21, + messageId: 'expectedTypedef', + }, + ], + }, + { + code: `class Test { + constructor(private param = 'something') {} + }`, + errors: [ + { + column: 21, + messageId: 'expectedTypedef', + }, + ], + }, // Method parameters { code: `class Test {