1

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

enter image description here

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234

1 Answers1

7

Those 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 comments) they're implicitly converted into strings.

cyr_x
  • 13,987
  • 2
  • 32
  • 46