Skip to content

const in class body #4045

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
nycdotnet opened this issue Jul 27, 2015 · 7 comments
Closed

const in class body #4045

nycdotnet opened this issue Jul 27, 2015 · 7 comments
Labels
Bug A bug in TypeScript Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@nycdotnet
Copy link

Hi,

I know ES6 doesn't support using const with class members and I'm assuming this is why TypeScript also does not. I'm wondering if the error if someone tries to do this with TypeScript can be improved.

class AtomicNumbers {
  static const H = 1;
  static const He = 2;
}

In the code above, TypeScript gives the error "'=' expected.", twice.

It would be much better to get an error such as "Class members cannot be declared with the const keyword.". Bonus points if it suggested something like, "Consider creating a module of the same name and exporting a constant from there."

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Jul 27, 2015
@mhegazy mhegazy added this to the Community milestone Jul 27, 2015
@mhegazy mhegazy added the Help Wanted You can do this label Jul 27, 2015
@MartyIX
Copy link
Contributor

MartyIX commented Sep 27, 2015

I'm a newbie but this is a result of my debugging analysis:

The problem is in parseClassElement() function:

  1. parseModifiers() function swallows modifiers that are not in proper context ( https://github.com/Microsoft/TypeScript/blob/master/src/compiler/parser.ts#L4714). So parseModifiers skips const.
  2. parsePropertyCall (https://github.com/Microsoft/TypeScript/blob/master/src/compiler/parser.ts#L4788) then assigns word const to name variable (instead of H as expected).

How to resolve the issue? I guess it is necessary to either:

or

  • add a new property modifiers.invalidModifiers in parseModifiers and then handle the errors for each case.

@DanielRosenwasser DanielRosenwasser added the Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". label Sep 28, 2015
@DanielRosenwasser
Copy link
Member

I would probably check if the current token is considered a keyword in any context and whether the subsequent token is a potential class member declaration or keyword on the same line. If so, then I would have a separate function that aggregates the invalid modifiers, reports errors on them, and returns the actual thing being declared.

So for the following:

class C {
    public const var export foo = 10;

    var constructor() { }
}

I would expect:

The 'const' keyword cannot appear on a class member.
The 'export' keyword cannot appear on a class member.
The 'var' keyword cannot appear on a class member.

I'm shying away from "A class member cannot have a '{0}' modifier" because it reads awkwardly if the modifier starts with a vowel. Maybe @JsonFreeman has suggestions all around.

@JsonFreeman
Copy link
Contributor

You could use "A class member cannot have the '{0}' modifier"

@DanielRosenwasser
Copy link
Member

Or specifically for the 'var' case, "A class member cannot have the '{0}' keyword."

Does that sound alright?

@JsonFreeman
Copy link
Contributor

Yes I think so.

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Dec 7, 2015
@DanielRosenwasser
Copy link
Member

We now give an error message thanks to @MartyIX.

@nycdotnet
Copy link
Author

@MartyIX way to go!

@mhegazy mhegazy modified the milestones: TypeScript 1.8, Community Dec 7, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

5 participants