TypeScript Dto类触发accessor-pairs错误:已定义Getter仍提示缺失?
问题根源及修复方案
ESLint的accessor-pairs规则要求类中定义的setter必须对应同名的getter(这里的“同名”指访问器的属性名一致,而非方法名带set/get前缀)。你当前的写法把setter命名为setEvent、getter命名为getEvent,规则会将二者判定为两个独立的方法,而非同一属性的读写访问器,因此触发错误。另外,setTransaction完全没有对应的getter,这也是报错的原因之一。
修复后的代码示例
import { Shop } from "@post-pricing/library/lib/src/orm/typeorm/entity"; import { parse } from "@post-pricing/library/lib/src/utility/date" export class ShopTransactionsDto { private event; private shopInfo: Shop; private transaction; constructor(){ this.event = null; this.shopInfo = null; this.transaction = null; } set event(event) { this.event = event; } get event() { return this.event; } set shopInfo(shopInfo: Shop){ this.shopInfo = shopInfo; } get shopInfo() { return this.shopInfo; } set transaction(transaction) { this.transaction = transaction; } get transaction() { return this.transaction; } }
说明
修改后每个setter都对应了同名的getter,accessor-pairs规则会正确识别这些成对的访问器,错误即可消除。
内容的提问来源于stack exchange,提问作者Heisenberg
相关产品推荐
相关产品推荐

