Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error report for equals instead of colon in object literals #25259

Closed
JoshuaKGoldberg opened this issue Jun 27, 2018 · 10 comments · Fixed by #38754 or #39418
Closed

Better error report for equals instead of colon in object literals #25259

JoshuaKGoldberg opened this issue Jun 27, 2018 · 10 comments · Fixed by #38754 or #39418
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Help Wanted You can do this

Comments

@JoshuaKGoldberg
Copy link
Contributor

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

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Jun 27, 2018
@Kingwl
Copy link
Contributor

Kingwl commented Jun 28, 2018

maybe provide a quick fix for that?

@DanielRosenwasser DanielRosenwasser added the Domain: Error Messages The issue relates to error messaging label Jun 28, 2018
@DanielRosenwasser
Copy link
Member

@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 };

@mhegazy
Copy link
Contributor

mhegazy commented Jun 28, 2018

Related #25201

@Kingwl
Copy link
Contributor

Kingwl commented Jun 29, 2018

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

@DanielRosenwasser
Copy link
Member

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!

@a-tarasyuk
Copy link
Contributor

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

@DanielRosenwasser
Copy link
Member

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?

@sandersn
Copy link
Member

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.

@DanielRosenwasser
Copy link
Member

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

@a-tarasyuk
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Help Wanted You can do this
Projects
None yet
7 participants