Skip to content

Commit 65a8c6c

Browse files
Handle numeric signs in 'isNumericName'.
To be fair, I don't think humanity knew about negative numbers until like 200 B.C.E.
1 parent 92a2c7f commit 65a8c6c

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4097,9 +4097,15 @@ module ts {
40974097
numericScanner = numericScanner || createScanner(compilerOptions.target || ScriptTarget.ES5, /*skipTrivia*/ false);
40984098
numericScanner.setText(name);
40994099

4100-
// Ensure that the name is nothing more than a numeric literal
4101-
// (i.e. it is preceded by nothing (whitespace) and scanning leaves us at the very end of the string).
4102-
return numericScanner.scan() === SyntaxKind.NumericLiteral && numericScanner.getTextPos() === name.length;
4100+
// Ensure that the name is nothing more than an optional sign (+/-) and a numeric literal
4101+
// (i.e. it is preceded by nothing and scanning leaves us at the very end of the string).
4102+
var token = numericScanner.scan();
4103+
4104+
if (token === SyntaxKind.MinusToken || token === SyntaxKind.PlusToken) {
4105+
token = numericScanner.scan();
4106+
}
4107+
4108+
return token === SyntaxKind.NumericLiteral && numericScanner.getTextPos() === name.length;
41034109
}
41044110

41054111
function checkObjectLiteral(node: ObjectLiteral, contextualMapper?: TypeMapper): Type {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(3,3): error TS2412: Property '"1"' of type 'string' is not assignable to numeric index type 'number'.
2+
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(4,3): error TS2412: Property '"-1"' of type 'string' is not assignable to numeric index type 'number'.
3+
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(5,3): error TS2412: Property '"+1"' of type 'string' is not assignable to numeric index type 'number'.
4+
5+
6+
==== tests/cases/compiler/propertiesAndIndexersForNumericNames.ts (3 errors) ====
7+
class C {
8+
[i: number]: number;
9+
public "1": string = "number"; // Error
10+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
!!! error TS2412: Property '"1"' of type 'string' is not assignable to numeric index type 'number'.
12+
public "-1": string = "negative number"; // Error
13+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
!!! error TS2412: Property '"-1"' of type 'string' is not assignable to numeric index type 'number'.
15+
public "+1": string = "positive number (for the paranoid)"; // Error
16+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17+
!!! error TS2412: Property '"+1"' of type 'string' is not assignable to numeric index type 'number'.
18+
19+
public " 1": string = "leading space"; // No error
20+
public "1 ": string = "trailing space"; // No error
21+
public "": string = "no nothing"; // No error
22+
public " ": string = "just space"; // No error
23+
public "1 0 1": string = "several numbers and spaces"; // No error
24+
public "NaN": string = "not a number"; // No error
25+
public "-NaN": string = "not a negative number"; // No error
26+
public "+Infinity": string = "A gillion"; // No error
27+
public "-Infinity": string = "Negative-a-gillion"; // No error
28+
}
29+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// [propertiesAndIndexersForNumericNames.ts]
2+
class C {
3+
[i: number]: number;
4+
public "1": string = "number"; // Error
5+
public "-1": string = "negative number"; // Error
6+
public "+1": string = "positive number (for the paranoid)"; // Error
7+
8+
public " 1": string = "leading space"; // No error
9+
public "1 ": string = "trailing space"; // No error
10+
public "": string = "no nothing"; // No error
11+
public " ": string = "just space"; // No error
12+
public "1 0 1": string = "several numbers and spaces"; // No error
13+
public "NaN": string = "not a number"; // No error
14+
public "-NaN": string = "not a negative number"; // No error
15+
public "+Infinity": string = "A gillion"; // No error
16+
public "-Infinity": string = "Negative-a-gillion"; // No error
17+
}
18+
19+
20+
//// [propertiesAndIndexersForNumericNames.js]
21+
var C = (function () {
22+
function C() {
23+
this["1"] = "number"; // Error
24+
this["-1"] = "negative number"; // Error
25+
this["+1"] = "positive number (for the paranoid)"; // Error
26+
this[" 1"] = "leading space"; // No error
27+
this["1 "] = "trailing space"; // No error
28+
this[""] = "no nothing"; // No error
29+
this[" "] = "just space"; // No error
30+
this["1 0 1"] = "several numbers and spaces"; // No error
31+
this["NaN"] = "not a number"; // No error
32+
this["-NaN"] = "not a negative number"; // No error
33+
this["+Infinity"] = "A gillion"; // No error
34+
this["-Infinity"] = "Negative-a-gillion"; // No error
35+
}
36+
return C;
37+
})();

0 commit comments

Comments
 (0)