You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:48:18