Home > Net >  Typescript error 1005. ':' expected when trying to access returned JSON data
Typescript error 1005. ':' expected when trying to access returned JSON data

Time:03-09

I'm returning data from an API call. The returned JSON looks like this:

{
    "data": {
        "getAccountDetails": {
            "cars": [
                {
                    "rental": true,
                    "id": "abcdefg"                    
                }
            ]
        }
    }
}

I'm trying to console log the boolean rental (true or false). I keep getting this typescript error: ':' expected. ts(1005. This is how I'm console logging:

console.log(data?.getAccountDetails?.cars?[0]);

How can I resolve this error?

CodePudding user response:

You are missing a dot.

console.log(data?.getThermostatDetails?.thermostats?.[0]);

It's ?. even when you want to access a property via square brackets.

  • Related