[英]How to define a type to computed properties in typescript?
我是 typescript 的新手,我正在将我的 javascript 代码转换为 typescript,这样我就可以学习打字稿了。
我有下面的代码,我在 [context.,label] 收到 typescript 警告错误,表明计算的属性名称必须是“string”、“number”、“symbol”类型。 或“任何”。
我的问题是如何解决这个问题以及为什么 typescript 会发出此警告错误? 我认为 typescript 与 javascript 差别不大?
预先感谢您的解释。
private initializeJoiError = (error: Joi.ValidationError) => {
this._JoiError =
{
status: "failed",
validationErrors: {
details: _.map(error.details, ({message, context}) => {
return {**[context!.label]** : message.replace(/['"]/g, "")}
})
}
}
}
我相信你可以使用Record :
//...
return {
[context!.label] : message.replace(/['"]/g, ""),
} as Record<typeof context['label'], string> //or whatever type that is
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.