如何避免typescript中的变量名被当作字符串处理?

新手上路,请多包涵

代码中有这样一段内容:

const time: string = new Date().toString()
console.log({ time: this.data })

我的本意是获得一个以当前时间戳为key的对象,形如:

{
"Tue Aug 03 2021 17:44:44 GMT+0800 (中国标准时间)": "xxxx"
}

但打印结果是:

{
"time": "xxxx"
}

请问该如何让编译器判定time是一个变量名而非字符串?

阅读 1.4k
1 个回答

这跟ts没啥关系

const time = new Date().toString()
console.log({ [time]: "XXXXX" })
推荐问题