'How to define a type to computed properties in typescript?
I'm new to typescript and i'm converting my javascript code to typescript so I can learn type script.
I have this below code and I'm receiving a typescript warning error at [context!.label] which indicates that a computed property name must be of type 'string', 'number', 'symbol', or 'any'.
My question is how I can fix this and why typescript is giving this warning error? I thought that typescript differs not so much from javascript?
Thank you in advanced for the explanation.
private initializeJoiError = (error: Joi.ValidationError) => {
this._JoiError =
{
status: "failed",
validationErrors: {
details: _.map(error.details, ({message, context}) => {
return {**[context!.label]** : message.replace(/['"]/g, "")}
})
}
}
}
Solution 1:[1]
I believe you can use Record:
//...
return {
[context!.label] : message.replace(/['"]/g, ""),
} as Record<typeof context['label'], string> //or whatever type that is
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | E. Berke Karagöz |
