'Error - Why am I receiving an syntax error warning when initialising an object

I'm new to type script and I'm learning it by trying to convert javascript files into type script.

I have come to a situation where I'm receiving an error warning at the colon symbol of an object:

[context!.label] **:** message.replace(/['"]/g, "")

JoiError definition is as following code:

private initializeJoiError = (error: Joi.ValidationError) => {
  this._JoiError = {
       status: "failed",
       validationErrors: {
            details: _.map(error.details, ({message, context}) => {
            [context!.label] : message.replace(/['"]/g, "")
           })
       }
  }
}

Can someone explain me why I'm receiving this error and how to fix it?


Solution 1:[1]

This happens because TypeScript interprets the innermost part of your nested function as a function body wrapped in a { ... } block.

Try returning an explicit object from that function instead:

private initializeJoiError = (error: Joi.ValidationError) => {
  this._JoiError = {
    status: "failed",
    validationErrors: {
      details: _.map(error.details, ({message, context}) => {
        return {[context!.label] : message.replace(/['"]/g, "")}
      })
    }
  }
}

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 Frost