@typescript-eslint/explicit-member-accessibility校验接口getter/setter报错如何解决
你遇到的报错不是配置错误,是你使用的@typescript-eslint旧版本的规则实现缺陷导致的。
问题原因
TypeScript语法明确规定,接口中的所有成员默认都是公共访问级别,不支持添加public/private/protected等访问修饰符,即使手动添加也会直接抛出TS语法错误。
你当前使用的@typescript-eslint/eslint-plugin4.x版本中,@typescript-eslint/explicit-member-accessibility规则没有针对接口、类型别名的成员做特殊豁免处理,错误地将类成员的检查逻辑套用在了接口成员上,才会触发这个不符合语法规则的报错。
解决方案
- 方案1:将
@typescript-eslint/eslint-plugin和@typescript-eslint/parser升级到5.0.0及以上版本,该问题已经在后续版本修复,规则会自动跳过对接口成员的访问修饰符检查。 - 方案2:如果暂时无法升级依赖,可以在现有规则配置中新增
ignoredInterfaces: true配置项,手动指定规则忽略对接口成员的检查,修改后的配置如下:
"@typescript-eslint/explicit-member-accessibility": [ "error", { "accessibility": "explicit", "overrides": { "accessors": "explicit", "constructors": "no-public", "methods": "explicit", "properties": "explicit", "parameterProperties": "explicit" }, "ignoredInterfaces": true } ]
内容的提问来源于stack exchange,提问作者Vladyslav Yazykov
相关产品推荐
相关产品推荐

