diff --git a/packages/eslint-plugin/src/rules/no-inferrable-types.ts b/packages/eslint-plugin/src/rules/no-inferrable-types.ts index 9adb4bca9ecd..e77b694cc707 100644 --- a/packages/eslint-plugin/src/rules/no-inferrable-types.ts +++ b/packages/eslint-plugin/src/rules/no-inferrable-types.ts @@ -46,6 +46,8 @@ export default util.createRule({ }, ], create(context, [{ ignoreParameters, ignoreProperties }]) { + const sourceCode = context.getSourceCode(); + function isFunctionCall( init: TSESTree.Expression, callName: string, @@ -215,7 +217,16 @@ export default util.createRule({ data: { type, }, - fix: fixer => fixer.remove(typeNode), + *fix(fixer) { + if ( + (node.type === AST_NODE_TYPES.AssignmentPattern && + node.left.optional) || + (node.type === AST_NODE_TYPES.PropertyDefinition && node.definite) + ) { + yield fixer.remove(sourceCode.getTokenBefore(typeNode)!); + } + yield fixer.remove(typeNode); + }, }); } 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 d85806b69c4e..cfea578b9f61 100644 --- a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts +++ b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts @@ -156,7 +156,54 @@ class Foo { invalid: [ ...invalidTestCases, - + { + // This is invalid TS semantic, but it's trivial to make valid anyway + code: 'const fn = (a?: number = 5) => {};', + output: 'const fn = (a = 5) => {};', + options: [ + { + ignoreParameters: false, + }, + ], + errors: [ + { + messageId: 'noInferrableType', + data: { + type: 'number', + }, + line: 1, + column: 13, + }, + ], + }, + { + // This is invalid TS semantic, but it's trivial to make valid anyway + code: ` +class A { + a!: number = 1; +} + `, + output: ` +class A { + a = 1; +} + `, + options: [ + { + ignoreProperties: false, + }, + ], + errors: [ + { + messageId: 'noInferrableType', + data: { + type: 'number', + }, + line: 3, + column: 3, + }, + ], + }, { code: "const fn = (a: number = 5, b: boolean = true, c: string = 'foo') => {};", output: "const fn = (a = 5, b = true, c = 'foo') => {};", pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy