Skip to content

Commit 92a2c7f

Browse files
Use our own scanner for 'isNumericName'.
1 parent e5b6bfb commit 92a2c7f

File tree

5 files changed

+88
-5
lines changed

5 files changed

+88
-5
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4092,8 +4092,14 @@ module ts {
40924092
return createArrayType(elementType);
40934093
}
40944094

4095+
var numericScanner: Scanner;
40954096
function isNumericName(name: string) {
4096-
return !isNaN(<number><any>name);
4097+
numericScanner = numericScanner || createScanner(compilerOptions.target || ScriptTarget.ES5, /*skipTrivia*/ false);
4098+
numericScanner.setText(name);
4099+
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;
40974103
}
40984104

40994105
function checkObjectLiteral(node: ObjectLiteral, contextualMapper?: TypeMapper): Type {

tests/baselines/reference/propertiesAndIndexers.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ tests/cases/compiler/propertiesAndIndexers.ts(33,11): error TS2411: Property '6'
1313
tests/cases/compiler/propertiesAndIndexers.ts(33,11): error TS2413: Numeric index type 'string' is not assignable to string index type 'number'.
1414
tests/cases/compiler/propertiesAndIndexers.ts(34,5): error TS2411: Property '2' of type 'Z' is not assignable to string index type 'number'.
1515
tests/cases/compiler/propertiesAndIndexers.ts(34,5): error TS2412: Property '2' of type 'Z' is not assignable to numeric index type 'string'.
16-
tests/cases/compiler/propertiesAndIndexers.ts(35,5): error TS2412: Property 'Infinity' of type 'number' is not assignable to numeric index type 'string'.
1716
tests/cases/compiler/propertiesAndIndexers.ts(36,5): error TS2411: Property 'zoo' of type 'string' is not assignable to string index type 'number'.
1817
tests/cases/compiler/propertiesAndIndexers.ts(44,5): error TS2411: Property 't' of type 'number' is not assignable to string index type 'string'.
1918
tests/cases/compiler/propertiesAndIndexers.ts(50,5): error TS2412: Property '3' of type 'boolean' is not assignable to numeric index type 'string'.
2019

2120

22-
==== tests/cases/compiler/propertiesAndIndexers.ts (19 errors) ====
21+
==== tests/cases/compiler/propertiesAndIndexers.ts (18 errors) ====
2322
interface X { }
2423
interface Y {
2524
n: number;
@@ -85,8 +84,6 @@ tests/cases/compiler/propertiesAndIndexers.ts(50,5): error TS2412: Property '3'
8584
~~~~~
8685
!!! error TS2412: Property '2' of type 'Z' is not assignable to numeric index type 'string'.
8786
Infinity: number;
88-
~~~~~~~~~~~~~~~~~
89-
!!! error TS2412: Property 'Infinity' of type 'number' is not assignable to numeric index type 'string'.
9087
zoo: string;
9188
~~~~~~~~~~~~
9289
!!! error TS2411: Property 'zoo' of type 'string' is not assignable to string index type 'number'.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
tests/cases/compiler/propertiesAndIndexers2.ts(2,5): error TS2413: Numeric index type 'string' is not assignable to string index type 'number'.
2+
tests/cases/compiler/propertiesAndIndexers2.ts(8,5): error TS2411: Property 'c' of type 'string' is not assignable to string index type 'number'.
3+
tests/cases/compiler/propertiesAndIndexers2.ts(9,5): error TS2411: Property '3' of type 'string' is not assignable to string index type 'number'.
4+
tests/cases/compiler/propertiesAndIndexers2.ts(10,5): error TS2411: Property 'Infinity' of type 'string' is not assignable to string index type 'number'.
5+
tests/cases/compiler/propertiesAndIndexers2.ts(11,5): error TS2411: Property '"-Infinity"' of type 'string' is not assignable to string index type 'number'.
6+
tests/cases/compiler/propertiesAndIndexers2.ts(12,5): error TS2411: Property 'NaN' of type 'string' is not assignable to string index type 'number'.
7+
tests/cases/compiler/propertiesAndIndexers2.ts(13,5): error TS2411: Property '"-NaN"' of type 'string' is not assignable to string index type 'number'.
8+
tests/cases/compiler/propertiesAndIndexers2.ts(14,5): error TS2411: Property '6' of type '() => string' is not assignable to string index type 'number'.
9+
tests/cases/compiler/propertiesAndIndexers2.ts(14,5): error TS2412: Property '6' of type '() => string' is not assignable to numeric index type 'string'.
10+
11+
12+
==== tests/cases/compiler/propertiesAndIndexers2.ts (9 errors) ====
13+
interface A {
14+
[n: number]: string;
15+
~~~~~~~~~~~~~~~~~~~~
16+
!!! error TS2413: Numeric index type 'string' is not assignable to string index type 'number'.
17+
[s: string]: number;
18+
}
19+
20+
// All of these should fail.
21+
interface B extends A {
22+
c: string;
23+
~~~~~~~~~~
24+
!!! error TS2411: Property 'c' of type 'string' is not assignable to string index type 'number'.
25+
3: string;
26+
~~~~~~~~~~
27+
!!! error TS2411: Property '3' of type 'string' is not assignable to string index type 'number'.
28+
Infinity: string;
29+
~~~~~~~~~~~~~~~~~
30+
!!! error TS2411: Property 'Infinity' of type 'string' is not assignable to string index type 'number'.
31+
"-Infinity": string;
32+
~~~~~~~~~~~~~~~~~~~~
33+
!!! error TS2411: Property '"-Infinity"' of type 'string' is not assignable to string index type 'number'.
34+
NaN: string;
35+
~~~~~~~~~~~~
36+
!!! error TS2411: Property 'NaN' of type 'string' is not assignable to string index type 'number'.
37+
"-NaN": string;
38+
~~~~~~~~~~~~~~~
39+
!!! error TS2411: Property '"-NaN"' of type 'string' is not assignable to string index type 'number'.
40+
6(): string;
41+
~~~~~~~~~~~~
42+
!!! error TS2411: Property '6' of type '() => string' is not assignable to string index type 'number'.
43+
~~~~~~~~~~~~
44+
!!! error TS2412: Property '6' of type '() => string' is not assignable to numeric index type 'string'.
45+
}
46+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [propertiesAndIndexers2.ts]
2+
interface A {
3+
[n: number]: string;
4+
[s: string]: number;
5+
}
6+
7+
// All of these should fail.
8+
interface B extends A {
9+
c: string;
10+
3: string;
11+
Infinity: string;
12+
"-Infinity": string;
13+
NaN: string;
14+
"-NaN": string;
15+
6(): string;
16+
}
17+
18+
19+
//// [propertiesAndIndexers2.js]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
interface A {
2+
[n: number]: string;
3+
[s: string]: number;
4+
}
5+
6+
// All of these should fail.
7+
interface B extends A {
8+
c: string;
9+
3: string;
10+
Infinity: string;
11+
"-Infinity": string;
12+
NaN: string;
13+
"-NaN": string;
14+
6(): string;
15+
}

0 commit comments

Comments
 (0)