如何配置ESLint解决no-inferrable-types与typedef规则冲突?
解决ESLint中@typescript-eslint/typedef与no-inferrable-types的规则冲突
你的核心需求是仅在类型无法自动推断时要求添加类型注解,当前两个规则冲突的原因是@typescript-eslint/typedef强制给所有匹配的声明加类型,而@typescript-eslint/no-inferrable-types又要求移除可推断的类型注解。要实现需求,只需调整@typescript-eslint/typedef的配置,开启ignoreInferredTypes选项,同时保留两个规则的必要设置即可。
最终配置如下:
{ "@typescript-eslint/no-inferrable-types": [ "error", { "ignoreParameters": true } ], "@typescript-eslint/typedef": [ "error", { "memberVariableDeclaration": true, "parameter": true, "propertyDeclaration": true, // 忽略可自动推断类型的声明,避免与no-inferrable-types冲突 "ignoreInferredTypes": true } ] }
配置说明:
@typescript-eslint/no-inferrable-types:保留原有ignoreParameters: true(若需要严格移除可推断的参数类型注解,可改为false),作用是移除不必要的、可自动推断的类型注解。@typescript-eslint/typedef:新增ignoreInferredTypes: true,该选项会让规则仅在类型无法自动推断时(比如无初始化值的成员变量、无默认值的参数)要求添加类型注解,完美匹配需求的同时避免与前者冲突。
内容的提问来源于stack exchange,提问作者moi_meme
相关产品推荐
相关产品推荐

