Property name on object from variable Property name on object from variable typescript typescript

Property name on object from variable


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        }    };}


You can do it like this:

export function objectFactory(prop: string) {    let data: any = {};    data[prop] = {};    data[prop].valid = false;    return data;}