You need to enable JavaScript to run this app. TypeScript类构造函数中简化属性赋值、减少代码行数的方法
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript类构造函数中简化属性赋值、减少代码行数的方法

优化方案

以下是三种常用的优化方式,可根据需求选择:

方案1:使用Object.assign一行完成赋值(最常用,无额外学习成本)

直接在构造函数中使用JS内置的对象属性拷贝方法批量赋值,不需要逐行写赋值语句,代码如下:

interface requestQAttributes {
    id: number;
    uri?: string;
    groupId?: string;
    status?: string;
    tryCount?: number;
    createdDate: Date;
    startDate?: Date;
    completedDate?: Date;
    bodyType?: string;
    formData?: string;
    pageResponse?: string;
    domain?: string;
}

class RequestQ implements requestQAttributes {
    id: number;
    uri?: string;
    groupId?: string;
    status?: string;
    tryCount?: number;
    createdDate: Date;
    startDate?: Date;
    completedDate?: Date;
    bodyType?: string;
    formData?: string;
    pageResponse?: string;
    domain?: string;

    constructor(requestQ: requestQAttributes) {
        // 一行完成所有属性赋值
        Object.assign(this, requestQ);
    }
}

注意:如果需要给部分属性设置默认值,只需要在Object.assign执行前给this对应属性赋值即可,不会被入参的undefined覆盖。


方案2:同时简化类属性声明+赋值(进一步压缩重复代码)

如果不想在类里重复写和接口完全一致的属性声明,可以配合TS的declare修饰符简化,代码更短:

interface requestQAttributes {
    id: number;
    uri?: string;
    groupId?: string;
    status?: string;
    tryCount?: number;
    createdDate: Date;
    startDate?: Date;
    completedDate?: Date;
    bodyType?: string;
    formData?: string;
    pageResponse?: string;
    domain?: string;
}

class RequestQ implements requestQAttributes {
    declare id: number;
    declare uri?: string;
    declare groupId?: string;
    declare status?: string;
    declare tryCount?: number;
    declare createdDate: Date;
    declare startDate?: Date;
    declare completedDate?: Date;
    declare bodyType?: string;
    declare formData?: string;
    declare pageResponse?: string;
    declare domain?: string;

    constructor(requestQ: requestQAttributes) {
        Object.assign(this, requestQ);
    }
}

如果需要严格过滤入参的额外属性,避免意外赋值到实例上,可以用属性白名单遍历赋值:

const ALLOWED_KEYS: Array<keyof requestQAttributes> = ['id', 'uri', 'groupId', 'status', 'tryCount', 'createdDate', 'startDate', 'completedDate', 'bodyType', 'formData', 'pageResponse', 'domain'];

class RequestQ implements requestQAttributes {
    // 省略属性声明
    constructor(requestQ: requestQAttributes) {
        ALLOWED_KEYS.forEach(key => {
            this[key] = requestQ[key];
        });
    }
}

方案3:TS参数属性语法(适合入参为零散参数的场景)

如果可以接受把构造函数入参从对象改成零散参数,可以用TS特有的参数属性语法,不需要单独写类属性声明和赋值语句,TS会自动完成:

class RequestQ {
    constructor(
        public id: number,
        public createdDate: Date,
        public uri?: string,
        public groupId?: string,
        public status?: string,
        public tryCount?: number,
        public startDate?: Date,
        public completedDate?: Date,
        public bodyType?: string,
        public formData?: string,
        public pageResponse?: string,
        public domain?: string
    ) {}
}

内容的提问来源于stack exchange,提问作者Vikas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.17 18:15:06