Typescript: Better error report for equals instead of colon in object literals

Created on 27 Jun 2018  路  10Comments  路  Source: microsoft/TypeScript

TypeScript Version: 3.0.0-dev.201xxxxx


Search Terms: object literal cannot find name equals colon

Code

const container = {
    a: "apple",
    b = "banana",
};

Expected behavior:

Use a : please!

Actual behavior:

Cannot find name 'b'.
'=' can only be used in an object literal property inside a destructuring assignment.

Playground Link: http://www.typescriptlang.org/play/#src=const%20container%20%3D%20%7B%0D%0A%20%20%20%20a%3A%20%22apple%22%2C%0D%0A%20%20%20%20b%20%3D%20%22banana%22%2C%0D%0A%7D%3B

Related Issues: #25257

Bug Error Messages help wanted

Most helpful comment

@Kingwl Maybe, but I think reporting a better error first like

'=' can only be used in an object literal property inside a destructuring assignment. Did you mean to use a ':'?

would be more ideal. Keep in mind that if there is already a :, you really don't want to provide the new error:

let x = { foo: bar = 10 };

All 10 comments

maybe provide a quick fix for that?

@Kingwl Maybe, but I think reporting a better error first like

'=' can only be used in an object literal property inside a destructuring assignment. Did you mean to use a ':'?

would be more ideal. Keep in mind that if there is already a :, you really don't want to provide the new error:

let x = { foo: bar = 10 };

after track, i saw this pattern is included in ShorthandPropertyAssignment,
could we extract that and mixin with PropertyAssignment to handle this issue?

New message is

Did you mean to use a ':'? When following property names in an object literal, '=' implies a destructuring assignment.

Open to switching it in the future to

Did you mean to use a ':'? An '=' can only follow a property name when the containing object literal is part of a destructuring pattern.

Thanks @a-tarasyuk!

@DanielRosenwasser I could change the message in PR :(. I'll open a new PR for the new message. Is it ok?

I felt bad making you wait any longer to get an already-good change in. 馃槵

Do you think the new message is even better? What about you @sandersn?

Ehhhhh, it's slightly simpler but also slightly longer. I wouldn't change it if I were me, but, like, half of that is because the longer wording has a horizontal scroll bar on my browser.

It's up to you @a-tarasyuk, I'd merge it in if you think it's good and send the change.

I felt bad making you wait any longer to get an already-good change in.

@DanielRosenwasser Don't worry about that 馃槃 . I think it is good, I'll change the message.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Roam-Cooper picture Roam-Cooper  路  3Comments

blendsdk picture blendsdk  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

manekinekko picture manekinekko  路  3Comments

wmaurer picture wmaurer  路  3Comments