Skip to content

Improve error messages for computed class property names #42619

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

Closed
craigphicks opened this issue Feb 2, 2021 · 4 comments · Fixed by #42675
Closed

Improve error messages for computed class property names #42619

craigphicks opened this issue Feb 2, 2021 · 4 comments · Fixed by #42675
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements Good First Issue Well scoped, documented and has the green light Help Wanted You can do this

Comments

@craigphicks
Copy link

craigphicks commented Feb 2, 2021

Bug Report

🔎 Search Terms

"A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type. "

"ts(1166)"

🕗 Version & Regression Information

  • I was unable to test this on prior versions because unlikely to be differenct

⏯ Playground Link

Playground link example giving error message

💻 Code

const s=Symbol('foo')
const sf=Symbol.for('foo')
class C1 {
  static readonly ['foo']:Record<string,any>
  static readonly [s]: Record<string,any>
  static readonly [sf]: Record<string,any>
  static readonly [Symbol.for('foo')]: Record<string,any>  // ts1166
}

declare var o: {prop: 'foo'};

class C2 {
    [o.prop] = 1; // works
    [(o).prop] = 1; // ts1166, adding parens gives an error because it's syntactically no longer a dotted name
}

See this closed issue for the source the examples, and the reasoning why error (ts1166) is reasonable enough in both cases (although the error message content is not reasonably straightforward).

🙁 Actual behavior

Error message: ts1166
"A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type. "

🙂 Expected behavior

An error message like
"A computed property name in a class property declaration must be
a straightforward expression for a literal string or a literal number, or a constant reference to a symbol. "

"Straightforward" is obviously not a complete description, but it is accurate and fits in the error message.
Error ts1166 is clearly generated during syntax parsing, so claims about 'type' are bound to be wrong when the type is obscured by non-straightforward syntax.

BTW, 'unique symbol type' seems to be an alias for 'type of a literal symbol`, and isn't particularly helpful - every symbol instance is unique, just like every object is unique, and (AFAIK but I could be wrong) 'unique object type' is not a standard phrase. But ts1166 doesn't need to mention type anyway.

@DanielRosenwasser
Copy link
Member

How about

A computed property name in a class property declaration must have a simple literal type or a unique symbol type.

@DanielRosenwasser
Copy link
Member

Or

A computed class property name must have a simple literal type or a unique symbol type.

@DanielRosenwasser DanielRosenwasser changed the title change message for (ts1166) to be straightforward Improve error messages for computed class property names Feb 3, 2021
@DanielRosenwasser DanielRosenwasser added Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements Good First Issue Well scoped, documented and has the green light Help Wanted You can do this labels Feb 3, 2021
@DanielRosenwasser
Copy link
Member

@jonhue this one's close in spirit to #42523.

@jonhue
Copy link
Contributor

jonhue commented Feb 3, 2021

Thanks! I'll see if I find the time to look into this in the next couple of days!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants