错误: [no-confusing-void-expression] 自动修复在声明的返回类型为 `unknown` 时会产生 TS2355
作者: nazsats创建于 2026年8月25日更新于 2026年9月24日
标签bugpackage: eslint-pluginaccepting prs
Before You File a Bug Report Please Confirm You Have Done The Following... - [x] I have tried restarting my IDE and the issue persists. - [x] I have updated to the latest version of the packages. - [x] I have searched for related issues and found none that matched my issue. - [x] I have read the FAQ and my problem is not listed. - [x] If I used AI assistance to prepare this issue, I have reviewed and followed the AI Contribution Policy. ### Playground Link https://TypeScript-eslint.io/play#ts=6.0.2&showAST=ts&fileType=.tsx&code=BQSgXABArgdg1jA9gdxhAvAPggY0TAZ0QBsBTAOmMQHNgByAM0UTpAG4g&eslintrc=N4KABGBEBOCuA2BTAzpAXGYBfEWg&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false ### Repro Code () : unknown => console.log('foo'); ### ESLint Config module.exports = { parser: "@TypeScript-eslint/parser", rules: { "@TypeScript-eslint/no-confusing-void-expression": ["error"], } }; ### tsconfig { "compilerOptions": { "strict": true } } ### Expected Result - the rule here is defining as the arrow does return a void expression. - the autofix is not, because the fixed output no longer compiles - when the fix would break the code, the rule already knows to withhold it: that is what canFix is for, and what #7674 added it for. - so the expectation is no autofix offered in this case, same as the conditionally-void ternary cases ### Actual Result The facts, again in your own words: - reported: "Returning a void expression from an arrow function shorthand is forbidden. Please add braces to the arrow function." - autofix produces () : unknown => { console.log('foo'); }; - that output fails with TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value ### Additional Info Worth including, because these are the parts that make it easy to act on: - It is specific to unknown. Controls with void and any fix cleanly: - () : void => console.log('foo'); -> (): void => { console.log('foo'); }; compiles - () : any => console.log('foo'); -> (): any => { console.log('foo'); }; compiles - () : unknown => console.log('foo');-> (): unknown => { console.log('foo'); }; TS2355 - Three shapes, same cause. All three are already invalid fixtures in no-confusing-void-expression.test.ts: - () : unknown => console.log('foo'); - type Foo = unknown; - () : Foo => console.log(); - function test(): unknown { return console.log(); } - The alias case matters: the check has to resolve the annotation's type through the checker, not match the unknown keyword in the AST. - Their tests pass anyway, because RuleTester asserts the output...
内容来源: typescript-eslint/typescript-eslint