Why typescript allows class field name in number
1: number = 1;
2: number = 2;
As per this discussion Why can't variable names start with numbers? We can't create a class field name starting with number(even JavaScript
also does not allow, if we define a variable name is starting with number). But in typescript we can create a class field name starting with number(whatever). Why? and it's a bug in typescript
?.
Reference
Why typescript allows class field name in number
1: number = 1;
2: number = 2;
As per this discussion Why can't variable names start with numbers? We can't create a class field name starting with number(even JavaScript
also does not allow, if we define a variable name is starting with number). But in typescript we can create a class field name starting with number(whatever). Why? and it's a bug in typescript
?.
Reference
Share Improve this question edited Aug 3, 2017 at 11:32 Ramesh Rajendran asked Aug 3, 2017 at 10:32 Ramesh RajendranRamesh Rajendran 38.7k49 gold badges159 silver badges240 bronze badges 6- 1 it doesn't, I'm not sure how you get that working? in the playground at least it doesn't typescriptlang/play/… – toskv Commented Aug 3, 2017 at 10:34
- It does not accept inside the method. Am asking about when you declare the variable globally. – Ramesh Rajendran Commented Aug 3, 2017 at 10:37
- Even then it does not work. Provide a plete verifiable example. – Murat Karagöz Commented Aug 3, 2017 at 10:38
- @toskv and Murat I have attached the reference. Could you please explain for the down vote. – Ramesh Rajendran Commented Aug 3, 2017 at 10:40
- 1 Those are not variables but class fields. – zerkms Commented Aug 3, 2017 at 10:41
1 Answer
Reset to default 7Those definitions of 1
and 2
aren't variable definitions, you're defining class members, which is totally fine to name them by a number.
But remember you can't access them by this.1
you have to use this[1]
.
In Javascript object properties can be named by a number (which is for example what the implementation of Array
is doing), because (as @zerkms statet in the ments) they're implicitly converted into strings.