microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.19k stars 12.38k forks source link

" 1" with a space is treated like numeric property name #728

Closed osdm closed 9 years ago

osdm commented 9 years ago

Quoted literal property names with leading or trailing spaces are treated as numeric property names, although they are not treated as numbers by JS.

class A {
  [i: number]: number;
  public "1": string = "no space"; // Error here - OK
  public " 1": string = "space"; // Error here - Not expected?
}
var aaa = new A();
window.alert(aaa[1]); // Shows "no space" to confirm JS behaviour
DanielRosenwasser commented 9 years ago

I believe that technically, this isn't a bug, but it may prompt us to review the TypeScript spec. Perhaps @ahejlsberg, @JsonFreeman, or others can clarify.

3.7.4 Index Signatures

A _numerically named property_ is a property whose name is a valid numeric literal. Specifically, a property with a name N for which ToNumber(N) is not NaN, where ToNumber is the abstract operation defined in ECMAScript specification.

RyanCavanaugh commented 9 years ago

The definition should probably be tightened to only apply to properties x such that ToString(ToNumber(x)) === x. Similarly, names like 1e2 are not actually numerically-named for the purposes of runtime behavior.

JsonFreeman commented 9 years ago

Right, I remember discussing this with @RyanCavanaugh. The ecmaScript rules around this can get pretty weird, as 1e2 would not be the result of a ToString, but 1e25 can be.

DanielRosenwasser commented 9 years ago

Any reason this has the milestone of 2.0? This is a relatively minor spec change.

JsonFreeman commented 9 years ago

Agreed

ghost commented 9 years ago

@ahejlsberg, @osdm, this bug is not reproducible in Version 1.5.0-beta (npm install typescript@1.5.0-beta), fixed by #860.

DanielRosenwasser commented 9 years ago

Thanks @jasonwilliams200OK, right now this tracks the need to update the spec.

ahejlsberg commented 9 years ago

Fixed in #4033.