From a4e625fc97d05e6bce82f58cd2a877cc35a87820 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Thu, 29 Aug 2019 19:33:12 +0300 Subject: [PATCH] fix(eslint-plugin): [no-inferrable-types] ignore optional props (#918) --- packages/eslint-plugin/src/rules/no-inferrable-types.ts | 2 +- .../tests/rules/no-inferrable-types.test.ts | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/no-inferrable-types.ts b/packages/eslint-plugin/src/rules/no-inferrable-types.ts index 40fc6a60c7e..92a58252679 100644 --- a/packages/eslint-plugin/src/rules/no-inferrable-types.ts +++ b/packages/eslint-plugin/src/rules/no-inferrable-types.ts @@ -250,7 +250,7 @@ export default util.createRule({ // Essentially a readonly property without a type // will result in its value being the type, leading to // compile errors if the type is stripped. - if (ignoreProperties || node.readonly) { + if (ignoreProperties || node.readonly || node.optional) { return; } reportInferrableType(node, node.typeAnnotation, node.value); 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 fb2563ed4ee..2301d6eda5c 100644 --- a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts +++ b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts @@ -123,6 +123,15 @@ ruleTester.run('no-inferrable-types', rule, { "class Foo { a: number = 5; b: boolean = true; c: string = 'foo'; }", options: [{ ignoreProperties: true }], }, + { + code: ` +class Foo { + a?: number = 5; + b?: boolean = true; + c?: string = 'foo'; +} + `, + }, ], invalid: [