变量中对象的属性名称

变量中对象的属性名称

问题描述:

打字稿中是否可以通过变量设置属性名称?

Is there a way in typescript to set a property name from a variable?

类似这样的东西

export function objectFactory(prop: string) {
    return {
        prop: {
            valid: false
        }
    };
}

您正在寻找

You are looking for computed properties, this is an ES6 feature and not specific to TypeScript.

export function objectFactory(prop: string) {
    return {
        [prop]: {
            valid: false
        }
    };
}