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 +}