From 92a2c7ff3cd66e541efc7e3b49f7ba95e3b2f42c Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 8 Oct 2014 17:05:04 -0700 Subject: [PATCH 1/7] Use our own scanner for 'isNumericName'. --- src/compiler/checker.ts | 8 +++- .../propertiesAndIndexers.errors.txt | 5 +- .../propertiesAndIndexers2.errors.txt | 46 +++++++++++++++++++ .../reference/propertiesAndIndexers2.js | 19 ++++++++ .../cases/compiler/propertiesAndIndexers2.ts | 15 ++++++ 5 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/propertiesAndIndexers2.errors.txt create mode 100644 tests/baselines/reference/propertiesAndIndexers2.js create mode 100644 tests/cases/compiler/propertiesAndIndexers2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 33d4008885d..d7fe22ffdba 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4092,8 +4092,14 @@ module ts { return createArrayType(elementType); } + var numericScanner: Scanner; function isNumericName(name: string) { - return !isNaN(name); + numericScanner = numericScanner || createScanner(compilerOptions.target || ScriptTarget.ES5, /*skipTrivia*/ false); + numericScanner.setText(name); + + // Ensure that the name is nothing more than a numeric literal + // (i.e. it is preceded by nothing (whitespace) and scanning leaves us at the very end of the string). + return numericScanner.scan() === SyntaxKind.NumericLiteral && numericScanner.getTextPos() === name.length; } function checkObjectLiteral(node: ObjectLiteral, contextualMapper?: TypeMapper): Type { diff --git a/tests/baselines/reference/propertiesAndIndexers.errors.txt b/tests/baselines/reference/propertiesAndIndexers.errors.txt index 4ab8bba644c..bfe9c4219e4 100644 --- a/tests/baselines/reference/propertiesAndIndexers.errors.txt +++ b/tests/baselines/reference/propertiesAndIndexers.errors.txt @@ -13,13 +13,12 @@ tests/cases/compiler/propertiesAndIndexers.ts(33,11): error TS2411: Property '6' tests/cases/compiler/propertiesAndIndexers.ts(33,11): error TS2413: Numeric index type 'string' is not assignable to string index type 'number'. tests/cases/compiler/propertiesAndIndexers.ts(34,5): error TS2411: Property '2' of type 'Z' is not assignable to string index type 'number'. tests/cases/compiler/propertiesAndIndexers.ts(34,5): error TS2412: Property '2' of type 'Z' is not assignable to numeric index type 'string'. -tests/cases/compiler/propertiesAndIndexers.ts(35,5): error TS2412: Property 'Infinity' of type 'number' is not assignable to numeric index type 'string'. tests/cases/compiler/propertiesAndIndexers.ts(36,5): error TS2411: Property 'zoo' of type 'string' is not assignable to string index type 'number'. tests/cases/compiler/propertiesAndIndexers.ts(44,5): error TS2411: Property 't' of type 'number' is not assignable to string index type 'string'. tests/cases/compiler/propertiesAndIndexers.ts(50,5): error TS2412: Property '3' of type 'boolean' is not assignable to numeric index type 'string'. -==== tests/cases/compiler/propertiesAndIndexers.ts (19 errors) ==== +==== tests/cases/compiler/propertiesAndIndexers.ts (18 errors) ==== interface X { } interface Y { n: number; @@ -85,8 +84,6 @@ tests/cases/compiler/propertiesAndIndexers.ts(50,5): error TS2412: Property '3' ~~~~~ !!! error TS2412: Property '2' of type 'Z' is not assignable to numeric index type 'string'. Infinity: number; - ~~~~~~~~~~~~~~~~~ -!!! error TS2412: Property 'Infinity' of type 'number' is not assignable to numeric index type 'string'. zoo: string; ~~~~~~~~~~~~ !!! error TS2411: Property 'zoo' of type 'string' is not assignable to string index type 'number'. diff --git a/tests/baselines/reference/propertiesAndIndexers2.errors.txt b/tests/baselines/reference/propertiesAndIndexers2.errors.txt new file mode 100644 index 00000000000..e1ab8c9d13d --- /dev/null +++ b/tests/baselines/reference/propertiesAndIndexers2.errors.txt @@ -0,0 +1,46 @@ +tests/cases/compiler/propertiesAndIndexers2.ts(2,5): error TS2413: Numeric index type 'string' is not assignable to string index type 'number'. +tests/cases/compiler/propertiesAndIndexers2.ts(8,5): error TS2411: Property 'c' of type 'string' is not assignable to string index type 'number'. +tests/cases/compiler/propertiesAndIndexers2.ts(9,5): error TS2411: Property '3' of type 'string' is not assignable to string index type 'number'. +tests/cases/compiler/propertiesAndIndexers2.ts(10,5): error TS2411: Property 'Infinity' of type 'string' is not assignable to string index type 'number'. +tests/cases/compiler/propertiesAndIndexers2.ts(11,5): error TS2411: Property '"-Infinity"' of type 'string' is not assignable to string index type 'number'. +tests/cases/compiler/propertiesAndIndexers2.ts(12,5): error TS2411: Property 'NaN' of type 'string' is not assignable to string index type 'number'. +tests/cases/compiler/propertiesAndIndexers2.ts(13,5): error TS2411: Property '"-NaN"' of type 'string' is not assignable to string index type 'number'. +tests/cases/compiler/propertiesAndIndexers2.ts(14,5): error TS2411: Property '6' of type '() => string' is not assignable to string index type 'number'. +tests/cases/compiler/propertiesAndIndexers2.ts(14,5): error TS2412: Property '6' of type '() => string' is not assignable to numeric index type 'string'. + + +==== tests/cases/compiler/propertiesAndIndexers2.ts (9 errors) ==== + interface A { + [n: number]: string; + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2413: Numeric index type 'string' is not assignable to string index type 'number'. + [s: string]: number; + } + + // All of these should fail. + interface B extends A { + c: string; + ~~~~~~~~~~ +!!! error TS2411: Property 'c' of type 'string' is not assignable to string index type 'number'. + 3: string; + ~~~~~~~~~~ +!!! error TS2411: Property '3' of type 'string' is not assignable to string index type 'number'. + Infinity: string; + ~~~~~~~~~~~~~~~~~ +!!! error TS2411: Property 'Infinity' of type 'string' is not assignable to string index type 'number'. + "-Infinity": string; + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2411: Property '"-Infinity"' of type 'string' is not assignable to string index type 'number'. + NaN: string; + ~~~~~~~~~~~~ +!!! error TS2411: Property 'NaN' of type 'string' is not assignable to string index type 'number'. + "-NaN": string; + ~~~~~~~~~~~~~~~ +!!! error TS2411: Property '"-NaN"' of type 'string' is not assignable to string index type 'number'. + 6(): string; + ~~~~~~~~~~~~ +!!! error TS2411: Property '6' of type '() => string' is not assignable to string index type 'number'. + ~~~~~~~~~~~~ +!!! error TS2412: Property '6' of type '() => string' is not assignable to numeric index type 'string'. + } + \ No newline at end of file diff --git a/tests/baselines/reference/propertiesAndIndexers2.js b/tests/baselines/reference/propertiesAndIndexers2.js new file mode 100644 index 00000000000..74485a89248 --- /dev/null +++ b/tests/baselines/reference/propertiesAndIndexers2.js @@ -0,0 +1,19 @@ +//// [propertiesAndIndexers2.ts] +interface A { + [n: number]: string; + [s: string]: number; +} + +// All of these should fail. +interface B extends A { + c: string; + 3: string; + Infinity: string; + "-Infinity": string; + NaN: string; + "-NaN": string; + 6(): string; +} + + +//// [propertiesAndIndexers2.js] diff --git a/tests/cases/compiler/propertiesAndIndexers2.ts b/tests/cases/compiler/propertiesAndIndexers2.ts new file mode 100644 index 00000000000..188635bf1dd --- /dev/null +++ b/tests/cases/compiler/propertiesAndIndexers2.ts @@ -0,0 +1,15 @@ +interface A { + [n: number]: string; + [s: string]: number; +} + +// All of these should fail. +interface B extends A { + c: string; + 3: string; + Infinity: string; + "-Infinity": string; + NaN: string; + "-NaN": string; + 6(): string; +} From 5a49bb610aa05cff50301b44017392b9e18d1e1b Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 9 Oct 2014 00:33:35 -0700 Subject: [PATCH 2/7] Handle numeric signs in 'isNumericName'. To be fair, I don't think humanity knew about negative numbers until like 200 B.C.E. --- src/compiler/checker.ts | 12 ++++-- ...rtiesAndIndexersForNumericNames.errors.txt | 29 +++++++++++++++ .../propertiesAndIndexersForNumericNames.js | 37 +++++++++++++++++++ .../propertiesAndIndexersForNumericNames.ts | 16 ++++++++ 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt create mode 100644 tests/baselines/reference/propertiesAndIndexersForNumericNames.js create mode 100644 tests/cases/compiler/propertiesAndIndexersForNumericNames.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d7fe22ffdba..58fa72b684c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4097,9 +4097,15 @@ module ts { numericScanner = numericScanner || createScanner(compilerOptions.target || ScriptTarget.ES5, /*skipTrivia*/ false); numericScanner.setText(name); - // Ensure that the name is nothing more than a numeric literal - // (i.e. it is preceded by nothing (whitespace) and scanning leaves us at the very end of the string). - return numericScanner.scan() === SyntaxKind.NumericLiteral && numericScanner.getTextPos() === name.length; + // Ensure that the name is nothing more than an optional sign (+/-) and a numeric literal + // (i.e. it is preceded by nothing and scanning leaves us at the very end of the string). + var token = numericScanner.scan(); + + if (token === SyntaxKind.MinusToken || token === SyntaxKind.PlusToken) { + token = numericScanner.scan(); + } + + return token === SyntaxKind.NumericLiteral && numericScanner.getTextPos() === name.length; } function checkObjectLiteral(node: ObjectLiteral, contextualMapper?: TypeMapper): Type { diff --git a/tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt b/tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt new file mode 100644 index 00000000000..adbb159d469 --- /dev/null +++ b/tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt @@ -0,0 +1,29 @@ +tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(3,3): error TS2412: Property '"1"' of type 'string' is not assignable to numeric index type 'number'. +tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(4,3): error TS2412: Property '"-1"' of type 'string' is not assignable to numeric index type 'number'. +tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(5,3): error TS2412: Property '"+1"' of type 'string' is not assignable to numeric index type 'number'. + + +==== tests/cases/compiler/propertiesAndIndexersForNumericNames.ts (3 errors) ==== + class C { + [i: number]: number; + public "1": string = "number"; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property '"1"' of type 'string' is not assignable to numeric index type 'number'. + public "-1": string = "negative number"; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property '"-1"' of type 'string' is not assignable to numeric index type 'number'. + public "+1": string = "positive number (for the paranoid)"; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property '"+1"' of type 'string' is not assignable to numeric index type 'number'. + + public " 1": string = "leading space"; // No error + public "1 ": string = "trailing space"; // No error + public "": string = "no nothing"; // No error + public " ": string = "just space"; // No error + public "1 0 1": string = "several numbers and spaces"; // No error + public "NaN": string = "not a number"; // No error + public "-NaN": string = "not a negative number"; // No error + public "+Infinity": string = "A gillion"; // No error + public "-Infinity": string = "Negative-a-gillion"; // No error + } + \ No newline at end of file diff --git a/tests/baselines/reference/propertiesAndIndexersForNumericNames.js b/tests/baselines/reference/propertiesAndIndexersForNumericNames.js new file mode 100644 index 00000000000..36a3080ac38 --- /dev/null +++ b/tests/baselines/reference/propertiesAndIndexersForNumericNames.js @@ -0,0 +1,37 @@ +//// [propertiesAndIndexersForNumericNames.ts] +class C { + [i: number]: number; + public "1": string = "number"; // Error + public "-1": string = "negative number"; // Error + public "+1": string = "positive number (for the paranoid)"; // Error + + public " 1": string = "leading space"; // No error + public "1 ": string = "trailing space"; // No error + public "": string = "no nothing"; // No error + public " ": string = "just space"; // No error + public "1 0 1": string = "several numbers and spaces"; // No error + public "NaN": string = "not a number"; // No error + public "-NaN": string = "not a negative number"; // No error + public "+Infinity": string = "A gillion"; // No error + public "-Infinity": string = "Negative-a-gillion"; // No error +} + + +//// [propertiesAndIndexersForNumericNames.js] +var C = (function () { + function C() { + this["1"] = "number"; // Error + this["-1"] = "negative number"; // Error + this["+1"] = "positive number (for the paranoid)"; // Error + this[" 1"] = "leading space"; // No error + this["1 "] = "trailing space"; // No error + this[""] = "no nothing"; // No error + this[" "] = "just space"; // No error + this["1 0 1"] = "several numbers and spaces"; // No error + this["NaN"] = "not a number"; // No error + this["-NaN"] = "not a negative number"; // No error + this["+Infinity"] = "A gillion"; // No error + this["-Infinity"] = "Negative-a-gillion"; // No error + } + return C; +})(); diff --git a/tests/cases/compiler/propertiesAndIndexersForNumericNames.ts b/tests/cases/compiler/propertiesAndIndexersForNumericNames.ts new file mode 100644 index 00000000000..57fa1f730c0 --- /dev/null +++ b/tests/cases/compiler/propertiesAndIndexersForNumericNames.ts @@ -0,0 +1,16 @@ +class C { + [i: number]: number; + public "1": string = "number"; // Error + public "-1": string = "negative number"; // Error + public "+1": string = "positive number (for the paranoid)"; // Error + + public " 1": string = "leading space"; // No error + public "1 ": string = "trailing space"; // No error + public "": string = "no nothing"; // No error + public " ": string = "just space"; // No error + public "1 0 1": string = "several numbers and spaces"; // No error + public "NaN": string = "not a number"; // No error + public "-NaN": string = "not a negative number"; // No error + public "+Infinity": string = "A gillion"; // No error + public "-Infinity": string = "Negative-a-gillion"; // No error +} From 13b5fe05732d92954d62aa91c345c387ddf16cd6 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 9 Oct 2014 12:50:01 -0700 Subject: [PATCH 3/7] Ensure that numeric names are in their printed form. --- src/compiler/checker.ts | 16 ++- ...rConstrainsPropertyDeclarations.errors.txt | 11 +- ...ConstrainsPropertyDeclarations2.errors.txt | 11 +- ...rtiesAndIndexersForNumericNames.errors.txt | 81 ++++++++----- .../propertiesAndIndexersForNumericNames.js | 106 ++++++++++++------ .../propertiesAndIndexersForNumericNames.ts | 51 ++++++--- 6 files changed, 174 insertions(+), 102 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 58fa72b684c..fab031891a6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4094,6 +4094,13 @@ module ts { var numericScanner: Scanner; function isNumericName(name: string) { + // First see if the name is in canonical string representation. + // Unfortunately this permits various forms of "NaN" and "Infinity", + // but it is a good check to save time. + if ((+name).toString() !== name) { + return false; + } + numericScanner = numericScanner || createScanner(compilerOptions.target || ScriptTarget.ES5, /*skipTrivia*/ false); numericScanner.setText(name); @@ -4101,10 +4108,11 @@ module ts { // (i.e. it is preceded by nothing and scanning leaves us at the very end of the string). var token = numericScanner.scan(); - if (token === SyntaxKind.MinusToken || token === SyntaxKind.PlusToken) { + // '+' will never be in front of a number in its printed form. + if (token === SyntaxKind.MinusToken) { token = numericScanner.scan(); } - + return token === SyntaxKind.NumericLiteral && numericScanner.getTextPos() === name.length; } @@ -4158,7 +4166,9 @@ module ts { if (hasProperty(properties, id)) { if (kind === IndexKind.String || isNumericName(id)) { var type = getTypeOfSymbol(properties[id]); - if (!contains(propTypes, type)) propTypes.push(type); + if (!contains(propTypes, type)) { + propTypes.push(type); + } } } } diff --git a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt index fd23cab9956..dc45b3237d7 100644 --- a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt +++ b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt @@ -4,19 +4,16 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(90,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(93,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(18,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'. -tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(20,5): error TS2412: Property '"4.0"' of type 'number' is not assignable to numeric index type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(21,5): error TS2412: Property '3.0' of type 'MyNumber' is not assignable to numeric index type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(50,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'. -tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(55,5): error TS2412: Property '"4.0"' of type 'number' is not assignable to numeric index type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(68,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'. -tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(73,5): error TS2412: Property '"4.0"' of type 'number' is not assignable to numeric index type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(78,5): error TS2322: Type '{ [x: number]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: unknown; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }': Index signatures are incompatible: Type '{}' is not assignable to type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(88,9): error TS2304: Cannot find name 'Myn'. -==== tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts (14 errors) ==== +==== tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts (11 errors) ==== // String indexer types constrain the types of named properties in their containing type interface MyNumber extends Number { @@ -39,8 +36,6 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo !!! error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'. "3.0": string; // ok "4.0": number; // error - ~~~~~~~~~~~~~~ -!!! error TS2412: Property '"4.0"' of type 'number' is not assignable to numeric index type 'string'. 3.0: MyNumber // error ~~~~~~~~~~~~~ !!! error TS2412: Property '3.0' of type 'MyNumber' is not assignable to numeric index type 'string'. @@ -86,8 +81,6 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo foo(): string; // ok "3.0": string; // ok "4.0": number; // error - ~~~~~~~~~~~~~~ -!!! error TS2412: Property '"4.0"' of type 'number' is not assignable to numeric index type 'string'. f: MyNumber; // error } @@ -108,8 +101,6 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo foo(): string; // ok "3.0": string; // ok "4.0": number; // error - ~~~~~~~~~~~~~~ -!!! error TS2412: Property '"4.0"' of type 'number' is not assignable to numeric index type 'string'. f: MyNumber; // error } diff --git a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations2.errors.txt b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations2.errors.txt index 0e9cff15560..6eb6d80baf1 100644 --- a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations2.errors.txt +++ b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations2.errors.txt @@ -1,16 +1,13 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(16,5): error TS2412: Property '3.0' of type 'number' is not assignable to numeric index type 'A'. -tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(17,5): error TS2412: Property '"4.0"' of type 'string' is not assignable to numeric index type 'A'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(25,5): error TS2412: Property '3.0' of type 'number' is not assignable to numeric index type 'A'. -tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(26,5): error TS2412: Property '"4.0"' of type 'string' is not assignable to numeric index type 'A'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(34,5): error TS2412: Property '3.0' of type 'number' is not assignable to numeric index type 'A'. -tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(35,5): error TS2412: Property '"4.0"' of type 'string' is not assignable to numeric index type 'A'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(39,5): error TS2322: Type '{ [x: number]: {}; 1.0: A; 2.0: B; 3.0: number; "2.5": B; "4.0": string; }' is not assignable to type '{ [x: number]: A; }': Index signatures are incompatible: Type '{}' is not assignable to type 'A': Property 'foo' is missing in type '{}'. -==== tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts (7 errors) ==== +==== tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts (4 errors) ==== // String indexer providing a constraint of a user defined type class A { @@ -30,8 +27,6 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo ~~~~~~~~~~~~ !!! error TS2412: Property '3.0' of type 'number' is not assignable to numeric index type 'A'. "4.0": string; // error - ~~~~~~~~~~~~~~ -!!! error TS2412: Property '"4.0"' of type 'string' is not assignable to numeric index type 'A'. } interface Foo2 { @@ -43,8 +38,6 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo ~~~~~~~~~~~~ !!! error TS2412: Property '3.0' of type 'number' is not assignable to numeric index type 'A'. "4.0": string; // error - ~~~~~~~~~~~~~~ -!!! error TS2412: Property '"4.0"' of type 'string' is not assignable to numeric index type 'A'. } var a: { @@ -56,8 +49,6 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo ~~~~~~~~~~~~ !!! error TS2412: Property '3.0' of type 'number' is not assignable to numeric index type 'A'. "4.0": string; // error - ~~~~~~~~~~~~~~ -!!! error TS2412: Property '"4.0"' of type 'string' is not assignable to numeric index type 'A'. }; // error diff --git a/tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt b/tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt index adbb159d469..6fb16fb18fb 100644 --- a/tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt +++ b/tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt @@ -1,29 +1,54 @@ -tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(3,3): error TS2412: Property '"1"' of type 'string' is not assignable to numeric index type 'number'. -tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(4,3): error TS2412: Property '"-1"' of type 'string' is not assignable to numeric index type 'number'. -tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(5,3): error TS2412: Property '"+1"' of type 'string' is not assignable to numeric index type 'number'. - - -==== tests/cases/compiler/propertiesAndIndexersForNumericNames.ts (3 errors) ==== - class C { - [i: number]: number; - public "1": string = "number"; // Error - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2412: Property '"1"' of type 'string' is not assignable to numeric index type 'number'. - public "-1": string = "negative number"; // Error - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2412: Property '"-1"' of type 'string' is not assignable to numeric index type 'number'. - public "+1": string = "positive number (for the paranoid)"; // Error - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2412: Property '"+1"' of type 'string' is not assignable to numeric index type 'number'. - - public " 1": string = "leading space"; // No error - public "1 ": string = "trailing space"; // No error - public "": string = "no nothing"; // No error - public " ": string = "just space"; // No error - public "1 0 1": string = "several numbers and spaces"; // No error - public "NaN": string = "not a number"; // No error - public "-NaN": string = "not a negative number"; // No error - public "+Infinity": string = "A gillion"; // No error - public "-Infinity": string = "Negative-a-gillion"; // No error - } +tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(6,5): error TS2412: Property '"1"' of type 'string' is not assignable to numeric index type 'number'. +tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(7,5): error TS2412: Property '"-1"' of type 'string' is not assignable to numeric index type 'number'. +tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(8,5): error TS2412: Property '"-2.5"' of type 'string' is not assignable to numeric index type 'number'. +tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(9,5): error TS2412: Property '"3.141592"' of type 'string' is not assignable to numeric index type 'number'. +tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(10,5): error TS2412: Property '"1.2e-20"' of type 'string' is not assignable to numeric index type 'number'. + + +==== tests/cases/compiler/propertiesAndIndexersForNumericNames.ts (5 errors) ==== + class C { + [i: number]: number; + + // These all have numeric names; they should error + // because their types are not compatible with the numeric indexer. + public "1": string = "number"; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property '"1"' of type 'string' is not assignable to numeric index type 'number'. + public "-1": string = "negative number"; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property '"-1"' of type 'string' is not assignable to numeric index type 'number'. + public "-2.5": string = "negative number"; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property '"-2.5"' of type 'string' is not assignable to numeric index type 'number'. + public "3.141592": string = "pi-sitive number"; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property '"3.141592"' of type 'string' is not assignable to numeric index type 'number'. + public "1.2e-20": string = "really small number"; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property '"1.2e-20"' of type 'string' is not assignable to numeric index type 'number'. + + // These all have *partially* numeric names, + // but should really be treated as plain string literals. + public " 1": string = "leading space"; // No error + public "1 ": string = "trailing space"; // No error + public "": string = "no nothing"; // No error + public " ": string = "just space"; // No error + public "1 0 1": string = "several numbers and spaces"; // No error + public "NaN": string = "not a number"; // No error + public "-NaN": string = "not a negative number"; // No error + public "hunter2": string = "not a password"; // No error + public "+Infinity": string = "A gillion"; // No error + public "-Infinity": string = "Negative-a-gillion"; // No error + + // These fall into the above category, however, they are "trickier"; + // these all are *scanned* as numeric literals, but they are not written in + // "canonical" numeric representations. + public "+1": string = "positive number (for the paranoid)"; // No error + public "1e0": string = "just one"; // No error + public "-0e0": string = "just zero"; // No error + public "0xF00D": string = "hex food"; // No error + public "0xBEEF": string = "hex beef"; // No error + public "0123": string = "oct 83"; // No error + public "0.000000000000000000012": string = "should've been in exponential form"; // No error + } \ No newline at end of file diff --git a/tests/baselines/reference/propertiesAndIndexersForNumericNames.js b/tests/baselines/reference/propertiesAndIndexersForNumericNames.js index 36a3080ac38..c4e5aa40b31 100644 --- a/tests/baselines/reference/propertiesAndIndexersForNumericNames.js +++ b/tests/baselines/reference/propertiesAndIndexersForNumericNames.js @@ -1,37 +1,73 @@ -//// [propertiesAndIndexersForNumericNames.ts] +//// [propertiesAndIndexersForNumericNames.ts] class C { - [i: number]: number; - public "1": string = "number"; // Error - public "-1": string = "negative number"; // Error - public "+1": string = "positive number (for the paranoid)"; // Error - - public " 1": string = "leading space"; // No error - public "1 ": string = "trailing space"; // No error - public "": string = "no nothing"; // No error - public " ": string = "just space"; // No error - public "1 0 1": string = "several numbers and spaces"; // No error - public "NaN": string = "not a number"; // No error - public "-NaN": string = "not a negative number"; // No error - public "+Infinity": string = "A gillion"; // No error - public "-Infinity": string = "Negative-a-gillion"; // No error + [i: number]: number; + + // These all have numeric names; they should error + // because their types are not compatible with the numeric indexer. + public "1": string = "number"; // Error + public "-1": string = "negative number"; // Error + public "-2.5": string = "negative number"; // Error + public "3.141592": string = "pi-sitive number"; // Error + public "1.2e-20": string = "really small number"; // Error + + // These all have *partially* numeric names, + // but should really be treated as plain string literals. + public " 1": string = "leading space"; // No error + public "1 ": string = "trailing space"; // No error + public "": string = "no nothing"; // No error + public " ": string = "just space"; // No error + public "1 0 1": string = "several numbers and spaces"; // No error + public "NaN": string = "not a number"; // No error + public "-NaN": string = "not a negative number"; // No error + public "hunter2": string = "not a password"; // No error + public "+Infinity": string = "A gillion"; // No error + public "-Infinity": string = "Negative-a-gillion"; // No error + + // These fall into the above category, however, they are "trickier"; + // these all are *scanned* as numeric literals, but they are not written in + // "canonical" numeric representations. + public "+1": string = "positive number (for the paranoid)"; // No error + public "1e0": string = "just one"; // No error + public "-0e0": string = "just zero"; // No error + public "0xF00D": string = "hex food"; // No error + public "0xBEEF": string = "hex beef"; // No error + public "0123": string = "oct 83"; // No error + public "0.000000000000000000012": string = "should've been in exponential form"; // No error } - - -//// [propertiesAndIndexersForNumericNames.js] -var C = (function () { - function C() { - this["1"] = "number"; // Error - this["-1"] = "negative number"; // Error - this["+1"] = "positive number (for the paranoid)"; // Error - this[" 1"] = "leading space"; // No error - this["1 "] = "trailing space"; // No error - this[""] = "no nothing"; // No error - this[" "] = "just space"; // No error - this["1 0 1"] = "several numbers and spaces"; // No error - this["NaN"] = "not a number"; // No error - this["-NaN"] = "not a negative number"; // No error - this["+Infinity"] = "A gillion"; // No error - this["-Infinity"] = "Negative-a-gillion"; // No error - } - return C; -})(); + + +//// [propertiesAndIndexersForNumericNames.js] +var C = (function () { + function C() { + // These all have numeric names; they should error + // because their types are not compatible with the numeric indexer. + this["1"] = "number"; // Error + this["-1"] = "negative number"; // Error + this["-2.5"] = "negative number"; // Error + this["3.141592"] = "pi-sitive number"; // Error + this["1.2e-20"] = "really small number"; // Error + // These all have *partially* numeric names, + // but should really be treated as plain string literals. + this[" 1"] = "leading space"; // No error + this["1 "] = "trailing space"; // No error + this[""] = "no nothing"; // No error + this[" "] = "just space"; // No error + this["1 0 1"] = "several numbers and spaces"; // No error + this["NaN"] = "not a number"; // No error + this["-NaN"] = "not a negative number"; // No error + this["hunter2"] = "not a password"; // No error + this["+Infinity"] = "A gillion"; // No error + this["-Infinity"] = "Negative-a-gillion"; // No error + // These fall into the above category, however, they are "trickier"; + // these all are *scanned* as numeric literals, but they are not written in + // "canonical" numeric representations. + this["+1"] = "positive number (for the paranoid)"; // No error + this["1e0"] = "just one"; // No error + this["-0e0"] = "just zero"; // No error + this["0xF00D"] = "hex food"; // No error + this["0xBEEF"] = "hex beef"; // No error + this["0123"] = "oct 83"; // No error + this["0.000000000000000000012"] = "should've been in exponential form"; // No error + } + return C; +})(); diff --git a/tests/cases/compiler/propertiesAndIndexersForNumericNames.ts b/tests/cases/compiler/propertiesAndIndexersForNumericNames.ts index 57fa1f730c0..05e4e21e9a0 100644 --- a/tests/cases/compiler/propertiesAndIndexersForNumericNames.ts +++ b/tests/cases/compiler/propertiesAndIndexersForNumericNames.ts @@ -1,16 +1,35 @@ -class C { - [i: number]: number; - public "1": string = "number"; // Error - public "-1": string = "negative number"; // Error - public "+1": string = "positive number (for the paranoid)"; // Error - - public " 1": string = "leading space"; // No error - public "1 ": string = "trailing space"; // No error - public "": string = "no nothing"; // No error - public " ": string = "just space"; // No error - public "1 0 1": string = "several numbers and spaces"; // No error - public "NaN": string = "not a number"; // No error - public "-NaN": string = "not a negative number"; // No error - public "+Infinity": string = "A gillion"; // No error - public "-Infinity": string = "Negative-a-gillion"; // No error -} +class C { + [i: number]: number; + + // These all have numeric names; they should error + // because their types are not compatible with the numeric indexer. + public "1": string = "number"; // Error + public "-1": string = "negative number"; // Error + public "-2.5": string = "negative number"; // Error + public "3.141592": string = "pi-sitive number"; // Error + public "1.2e-20": string = "really small number"; // Error + + // These all have *partially* numeric names, + // but should really be treated as plain string literals. + public " 1": string = "leading space"; // No error + public "1 ": string = "trailing space"; // No error + public "": string = "no nothing"; // No error + public " ": string = "just space"; // No error + public "1 0 1": string = "several numbers and spaces"; // No error + public "NaN": string = "not a number"; // No error + public "-NaN": string = "not a negative number"; // No error + public "hunter2": string = "not a password"; // No error + public "+Infinity": string = "A gillion"; // No error + public "-Infinity": string = "Negative-a-gillion"; // No error + + // These fall into the above category, however, they are "trickier"; + // these all are *scanned* as numeric literals, but they are not written in + // "canonical" numeric representations. + public "+1": string = "positive number (for the paranoid)"; // No error + public "1e0": string = "just one"; // No error + public "-0e0": string = "just zero"; // No error + public "0xF00D": string = "hex food"; // No error + public "0xBEEF": string = "hex beef"; // No error + public "0123": string = "oct 83"; // No error + public "0.000000000000000000012": string = "should've been in exponential form"; // No error +} From c79619bce51b73aa67cbac5e00daa7dbba3c9606 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 9 Oct 2014 14:16:19 -0700 Subject: [PATCH 4/7] Quick edit to comment. --- src/compiler/checker.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index fab031891a6..a3d90017272 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4095,8 +4095,7 @@ module ts { var numericScanner: Scanner; function isNumericName(name: string) { // First see if the name is in canonical string representation. - // Unfortunately this permits various forms of "NaN" and "Infinity", - // but it is a good check to save time. + // We can't simply rely on this because this permits various forms of "NaN" and "Infinity". if ((+name).toString() !== name) { return false; } @@ -4104,7 +4103,7 @@ module ts { numericScanner = numericScanner || createScanner(compilerOptions.target || ScriptTarget.ES5, /*skipTrivia*/ false); numericScanner.setText(name); - // Ensure that the name is nothing more than an optional sign (+/-) and a numeric literal + // Ensure that the name is nothing more than an optional sign ('-') and a numeric literal // (i.e. it is preceded by nothing and scanning leaves us at the very end of the string). var token = numericScanner.scan(); From 9f32f64a48f941b8bdc4c4fbdd558204816b9d88 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 10 Oct 2014 14:12:21 -0700 Subject: [PATCH 5/7] Make things more conformant with 9.8.1 of EC-262 spec. Specifically, a numeric property is simply now any property whose name N is equal to ToString(ToNumber(N)). --- src/compiler/checker.ts | 21 +---------------- ...rConstrainsPropertyDeclarations.errors.txt | 2 +- .../propertiesAndIndexers.errors.txt | 5 +++- ...rtiesAndIndexersForNumericNames.errors.txt | 23 +++++++++++++++---- .../propertiesAndIndexersForNumericNames.js | 23 ++++++++++++++----- .../propertiesAndIndexersForNumericNames.ts | 12 +++++++--- 6 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7bf925bd8f2..dd5ad91a70c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4083,27 +4083,8 @@ module ts { return createArrayType(elementType); } - var numericScanner: Scanner; function isNumericName(name: string) { - // First see if the name is in canonical string representation. - // We can't simply rely on this because this permits various forms of "NaN" and "Infinity". - if ((+name).toString() !== name) { - return false; - } - - numericScanner = numericScanner || createScanner(compilerOptions.target || ScriptTarget.ES5, /*skipTrivia*/ false); - numericScanner.setText(name); - - // Ensure that the name is nothing more than an optional sign ('-') and a numeric literal - // (i.e. it is preceded by nothing and scanning leaves us at the very end of the string). - var token = numericScanner.scan(); - - // '+' will never be in front of a number in its printed form. - if (token === SyntaxKind.MinusToken) { - token = numericScanner.scan(); - } - - return token === SyntaxKind.NumericLiteral && numericScanner.getTextPos() === name.length; + return (+name).toString() === name; } function checkObjectLiteral(node: ObjectLiteral, contextualMapper?: TypeMapper): Type { diff --git a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt index 77411f2c7b2..080dc6316cf 100644 --- a/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt +++ b/tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt @@ -6,7 +6,7 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(18,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(21,5): error TS2412: Property '3.0' of type 'MyNumber' is not assignable to numeric index type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(50,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'. -tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(68,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'. +tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(68,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'. tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(78,5): error TS2322: Type '{ [x: number]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: any; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }': Index signatures are incompatible: Type '{}' is not assignable to type 'string'. diff --git a/tests/baselines/reference/propertiesAndIndexers.errors.txt b/tests/baselines/reference/propertiesAndIndexers.errors.txt index bfe9c4219e4..4ab8bba644c 100644 --- a/tests/baselines/reference/propertiesAndIndexers.errors.txt +++ b/tests/baselines/reference/propertiesAndIndexers.errors.txt @@ -13,12 +13,13 @@ tests/cases/compiler/propertiesAndIndexers.ts(33,11): error TS2411: Property '6' tests/cases/compiler/propertiesAndIndexers.ts(33,11): error TS2413: Numeric index type 'string' is not assignable to string index type 'number'. tests/cases/compiler/propertiesAndIndexers.ts(34,5): error TS2411: Property '2' of type 'Z' is not assignable to string index type 'number'. tests/cases/compiler/propertiesAndIndexers.ts(34,5): error TS2412: Property '2' of type 'Z' is not assignable to numeric index type 'string'. +tests/cases/compiler/propertiesAndIndexers.ts(35,5): error TS2412: Property 'Infinity' of type 'number' is not assignable to numeric index type 'string'. tests/cases/compiler/propertiesAndIndexers.ts(36,5): error TS2411: Property 'zoo' of type 'string' is not assignable to string index type 'number'. tests/cases/compiler/propertiesAndIndexers.ts(44,5): error TS2411: Property 't' of type 'number' is not assignable to string index type 'string'. tests/cases/compiler/propertiesAndIndexers.ts(50,5): error TS2412: Property '3' of type 'boolean' is not assignable to numeric index type 'string'. -==== tests/cases/compiler/propertiesAndIndexers.ts (18 errors) ==== +==== tests/cases/compiler/propertiesAndIndexers.ts (19 errors) ==== interface X { } interface Y { n: number; @@ -84,6 +85,8 @@ tests/cases/compiler/propertiesAndIndexers.ts(50,5): error TS2412: Property '3' ~~~~~ !!! error TS2412: Property '2' of type 'Z' is not assignable to numeric index type 'string'. Infinity: number; + ~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property 'Infinity' of type 'number' is not assignable to numeric index type 'string'. zoo: string; ~~~~~~~~~~~~ !!! error TS2411: Property 'zoo' of type 'string' is not assignable to string index type 'number'. diff --git a/tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt b/tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt index 6fb16fb18fb..01dcb61c70c 100644 --- a/tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt +++ b/tests/baselines/reference/propertiesAndIndexersForNumericNames.errors.txt @@ -3,9 +3,12 @@ tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(7,5): error TS2412: tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(8,5): error TS2412: Property '"-2.5"' of type 'string' is not assignable to numeric index type 'number'. tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(9,5): error TS2412: Property '"3.141592"' of type 'string' is not assignable to numeric index type 'number'. tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(10,5): error TS2412: Property '"1.2e-20"' of type 'string' is not assignable to numeric index type 'number'. +tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(11,5): error TS2412: Property '"Infinity"' of type 'string' is not assignable to numeric index type 'number'. +tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(12,5): error TS2412: Property '"-Infinity"' of type 'string' is not assignable to numeric index type 'number'. +tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(13,5): error TS2412: Property '"NaN"' of type 'string' is not assignable to numeric index type 'number'. -==== tests/cases/compiler/propertiesAndIndexersForNumericNames.ts (5 errors) ==== +==== tests/cases/compiler/propertiesAndIndexersForNumericNames.ts (8 errors) ==== class C { [i: number]: number; @@ -26,6 +29,15 @@ tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(10,5): error TS2412 public "1.2e-20": string = "really small number"; // Error ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2412: Property '"1.2e-20"' of type 'string' is not assignable to numeric index type 'number'. + public "Infinity": string = "A gillion"; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property '"Infinity"' of type 'string' is not assignable to numeric index type 'number'. + public "-Infinity": string = "Negative-a-gillion"; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property '"-Infinity"' of type 'string' is not assignable to numeric index type 'number'. + public "NaN": string = "not a number"; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2412: Property '"NaN"' of type 'string' is not assignable to numeric index type 'number'. // These all have *partially* numeric names, // but should really be treated as plain string literals. @@ -34,21 +46,24 @@ tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(10,5): error TS2412 public "": string = "no nothing"; // No error public " ": string = "just space"; // No error public "1 0 1": string = "several numbers and spaces"; // No error - public "NaN": string = "not a number"; // No error - public "-NaN": string = "not a negative number"; // No error public "hunter2": string = "not a password"; // No error public "+Infinity": string = "A gillion"; // No error - public "-Infinity": string = "Negative-a-gillion"; // No error + public "+NaN": string = "not a positive number"; // No error + public "-NaN": string = "not a negative number"; // No error + // These fall into the above category, however, they are "trickier"; // these all are *scanned* as numeric literals, but they are not written in // "canonical" numeric representations. public "+1": string = "positive number (for the paranoid)"; // No error public "1e0": string = "just one"; // No error + public "-0": string = "just zero"; // No error public "-0e0": string = "just zero"; // No error public "0xF00D": string = "hex food"; // No error public "0xBEEF": string = "hex beef"; // No error public "0123": string = "oct 83"; // No error + public "0o123": string = "explicit oct 83"; // No error + public "0b101101001010": string = "explicit binary"; // No error public "0.000000000000000000012": string = "should've been in exponential form"; // No error } \ No newline at end of file diff --git a/tests/baselines/reference/propertiesAndIndexersForNumericNames.js b/tests/baselines/reference/propertiesAndIndexersForNumericNames.js index c4e5aa40b31..975abdc00ec 100644 --- a/tests/baselines/reference/propertiesAndIndexersForNumericNames.js +++ b/tests/baselines/reference/propertiesAndIndexersForNumericNames.js @@ -9,6 +9,9 @@ class C { public "-2.5": string = "negative number"; // Error public "3.141592": string = "pi-sitive number"; // Error public "1.2e-20": string = "really small number"; // Error + public "Infinity": string = "A gillion"; // Error + public "-Infinity": string = "Negative-a-gillion"; // Error + public "NaN": string = "not a number"; // Error // These all have *partially* numeric names, // but should really be treated as plain string literals. @@ -17,21 +20,24 @@ class C { public "": string = "no nothing"; // No error public " ": string = "just space"; // No error public "1 0 1": string = "several numbers and spaces"; // No error - public "NaN": string = "not a number"; // No error - public "-NaN": string = "not a negative number"; // No error public "hunter2": string = "not a password"; // No error public "+Infinity": string = "A gillion"; // No error - public "-Infinity": string = "Negative-a-gillion"; // No error + public "+NaN": string = "not a positive number"; // No error + public "-NaN": string = "not a negative number"; // No error + // These fall into the above category, however, they are "trickier"; // these all are *scanned* as numeric literals, but they are not written in // "canonical" numeric representations. public "+1": string = "positive number (for the paranoid)"; // No error public "1e0": string = "just one"; // No error + public "-0": string = "just zero"; // No error public "-0e0": string = "just zero"; // No error public "0xF00D": string = "hex food"; // No error public "0xBEEF": string = "hex beef"; // No error public "0123": string = "oct 83"; // No error + public "0o123": string = "explicit oct 83"; // No error + public "0b101101001010": string = "explicit binary"; // No error public "0.000000000000000000012": string = "should've been in exponential form"; // No error } @@ -46,6 +52,9 @@ var C = (function () { this["-2.5"] = "negative number"; // Error this["3.141592"] = "pi-sitive number"; // Error this["1.2e-20"] = "really small number"; // Error + this["Infinity"] = "A gillion"; // Error + this["-Infinity"] = "Negative-a-gillion"; // Error + this["NaN"] = "not a number"; // Error // These all have *partially* numeric names, // but should really be treated as plain string literals. this[" 1"] = "leading space"; // No error @@ -53,20 +62,22 @@ var C = (function () { this[""] = "no nothing"; // No error this[" "] = "just space"; // No error this["1 0 1"] = "several numbers and spaces"; // No error - this["NaN"] = "not a number"; // No error - this["-NaN"] = "not a negative number"; // No error this["hunter2"] = "not a password"; // No error this["+Infinity"] = "A gillion"; // No error - this["-Infinity"] = "Negative-a-gillion"; // No error + this["+NaN"] = "not a positive number"; // No error + this["-NaN"] = "not a negative number"; // No error // These fall into the above category, however, they are "trickier"; // these all are *scanned* as numeric literals, but they are not written in // "canonical" numeric representations. this["+1"] = "positive number (for the paranoid)"; // No error this["1e0"] = "just one"; // No error + this["-0"] = "just zero"; // No error this["-0e0"] = "just zero"; // No error this["0xF00D"] = "hex food"; // No error this["0xBEEF"] = "hex beef"; // No error this["0123"] = "oct 83"; // No error + this["0o123"] = "explicit oct 83"; // No error + this["0b101101001010"] = "explicit binary"; // No error this["0.000000000000000000012"] = "should've been in exponential form"; // No error } return C; diff --git a/tests/cases/compiler/propertiesAndIndexersForNumericNames.ts b/tests/cases/compiler/propertiesAndIndexersForNumericNames.ts index 05e4e21e9a0..3a6f4afd52b 100644 --- a/tests/cases/compiler/propertiesAndIndexersForNumericNames.ts +++ b/tests/cases/compiler/propertiesAndIndexersForNumericNames.ts @@ -8,6 +8,9 @@ class C { public "-2.5": string = "negative number"; // Error public "3.141592": string = "pi-sitive number"; // Error public "1.2e-20": string = "really small number"; // Error + public "Infinity": string = "A gillion"; // Error + public "-Infinity": string = "Negative-a-gillion"; // Error + public "NaN": string = "not a number"; // Error // These all have *partially* numeric names, // but should really be treated as plain string literals. @@ -16,20 +19,23 @@ class C { public "": string = "no nothing"; // No error public " ": string = "just space"; // No error public "1 0 1": string = "several numbers and spaces"; // No error - public "NaN": string = "not a number"; // No error - public "-NaN": string = "not a negative number"; // No error public "hunter2": string = "not a password"; // No error public "+Infinity": string = "A gillion"; // No error - public "-Infinity": string = "Negative-a-gillion"; // No error + public "+NaN": string = "not a positive number"; // No error + public "-NaN": string = "not a negative number"; // No error + // These fall into the above category, however, they are "trickier"; // these all are *scanned* as numeric literals, but they are not written in // "canonical" numeric representations. public "+1": string = "positive number (for the paranoid)"; // No error public "1e0": string = "just one"; // No error + public "-0": string = "just zero"; // No error public "-0e0": string = "just zero"; // No error public "0xF00D": string = "hex food"; // No error public "0xBEEF": string = "hex beef"; // No error public "0123": string = "oct 83"; // No error + public "0o123": string = "explicit oct 83"; // No error + public "0b101101001010": string = "explicit binary"; // No error public "0.000000000000000000012": string = "should've been in exponential form"; // No error } From f933b58cf75998896ba277590e37fa4c41e9ce6f Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 13 Oct 2014 14:13:22 -0700 Subject: [PATCH 6/7] Explain what is happening in 'isNumericName'. --- src/compiler/checker.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index dd5ad91a70c..e0b565cab03 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4084,6 +4084,21 @@ module ts { } function isNumericName(name: string) { + // The intent of numeric names is that + // - they are names with text in a numeric form, and that + // - setting properties/indexing with them is always equivalent to doing so with the numeric literal 'numLit', + // acquired by applying the abstract 'ToNumber' operation on the name's text. + // + // The subtlety is in the latter portion, as we cannot reliably say that anything that looks like a numeric literal is a numeric name. + // In fact, it is the case that the text of the name must be equal to 'ToString(numLit)' for this to hold. + // + // Consider the property name '"0xF00D"'. When one indexes with '0xF00D', they are actually indexing with the value of 'ToString(0xF00D)' + // according to the ECMAScript specification, so it is actually as if the user indexed with the string '"61453"'. + // Thus, the text of all numeric literals equivalent to '61543' such as '0xF00D', '0xf00D', '0170015', etc. are not valid numeric names + // because their 'ToString' representation is not equal to their original text. + // This is motivated by ECMA-262 sections 9.3.1, 9.8.1, 11.1.5, and 11.2.1. + // + // Here, we test whether 'ToString(ToNumber(name))' is exactly equal to 'name'. return (+name).toString() === name; } From ea81831fb358e5917dcad507a74288418e31c87b Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 15 Oct 2014 15:15:08 -0700 Subject: [PATCH 7/7] More comments. --- src/compiler/checker.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e0b565cab03..f4f26fa4a3f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4099,6 +4099,12 @@ module ts { // This is motivated by ECMA-262 sections 9.3.1, 9.8.1, 11.1.5, and 11.2.1. // // Here, we test whether 'ToString(ToNumber(name))' is exactly equal to 'name'. + // The '+' prefix operator is equivalent here to applying the abstract ToNumber operation. + // Applying the 'toString()' method on a number gives us the abstract ToString operation on a number. + // + // Note that this accepts the values 'Infinity', '-Infinity', and 'NaN', and that this is intentional. + // This is desired behavior, because when indexing with them as numeric entities, you are indexing + // with the strings '"Infinity"', '"-Infinity"', and '"NaN"' respectively. return (+name).toString() === name; }