mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
Merge pull request #860 from Microsoft/numbersAreHard
Take runtime semantics of numerically named properties into account.
This commit is contained in:
commit
ad6203f4a3
@ -4675,7 +4675,28 @@ module ts {
|
||||
}
|
||||
|
||||
function isNumericName(name: string) {
|
||||
return (name !== "") && !isNaN(<number><any>name);
|
||||
// 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'.
|
||||
// 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;
|
||||
}
|
||||
|
||||
function checkObjectLiteral(node: ObjectLiteral, contextualMapper?: TypeMapper): Type {
|
||||
@ -4725,7 +4746,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,11 +1,10 @@
|
||||
tests/cases/compiler/enumIdentifierLiterals.ts(2,5): error TS1151: An enum member cannot have a numeric name.
|
||||
tests/cases/compiler/enumIdentifierLiterals.ts(3,5): error TS1151: An enum member cannot have a numeric name.
|
||||
tests/cases/compiler/enumIdentifierLiterals.ts(4,5): error TS1151: An enum member cannot have a numeric name.
|
||||
tests/cases/compiler/enumIdentifierLiterals.ts(5,5): error TS1151: An enum member cannot have a numeric name.
|
||||
tests/cases/compiler/enumIdentifierLiterals.ts(6,5): error TS1151: An enum member cannot have a numeric name.
|
||||
|
||||
|
||||
==== tests/cases/compiler/enumIdentifierLiterals.ts (5 errors) ====
|
||||
==== tests/cases/compiler/enumIdentifierLiterals.ts (4 errors) ====
|
||||
enum Nums {
|
||||
1.0,
|
||||
~~~
|
||||
@ -17,8 +16,6 @@ tests/cases/compiler/enumIdentifierLiterals.ts(6,5): error TS1151: An enum membe
|
||||
~~~~~~
|
||||
!!! error TS1151: An enum member cannot have a numeric name.
|
||||
"13e-1",
|
||||
~~~~~~~
|
||||
!!! error TS1151: An enum member cannot have a numeric name.
|
||||
0xF00D
|
||||
~~~~~~
|
||||
!!! error TS1151: An enum member cannot have a numeric name.
|
||||
|
||||
@ -4,12 +4,9 @@ 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]: string | 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 'string | number' is not assignable to type 'string':
|
||||
@ -17,7 +14,7 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo
|
||||
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 {
|
||||
@ -40,8 +37,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'.
|
||||
@ -87,8 +82,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
|
||||
}
|
||||
|
||||
@ -109,8 +102,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
|
||||
}
|
||||
|
||||
|
||||
@ -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]: string | number | A; 1.0: A; 2.0: B; 3.0: number; "2.5": B; "4.0": string; }' is not assignable to type '{ [x: number]: A; }':
|
||||
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations2.ts(39,5): error TS2322: Type '{ [x: number]: number | A; 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 'string | number | A' is not assignable to type 'A':
|
||||
Type 'string' is not assignable to type 'A'.
|
||||
Type 'number | A' is not assignable to type 'A':
|
||||
Type 'number' is not assignable to type 'A'.
|
||||
|
||||
|
||||
==== 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,17 +49,15 @@ 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
|
||||
var b: { [x: number]: A } = {
|
||||
~
|
||||
!!! error TS2322: Type '{ [x: number]: string | number | A; 1.0: A; 2.0: B; 3.0: number; "2.5": B; "4.0": string; }' is not assignable to type '{ [x: number]: A; }':
|
||||
!!! error TS2322: Type '{ [x: number]: number | A; 1.0: A; 2.0: B; 3.0: number; "2.5": B; "4.0": string; }' is not assignable to type '{ [x: number]: A; }':
|
||||
!!! error TS2322: Index signatures are incompatible:
|
||||
!!! error TS2322: Type 'string | number | A' is not assignable to type 'A':
|
||||
!!! error TS2322: Type 'string' is not assignable to type 'A'.
|
||||
!!! error TS2322: Type 'number | A' is not assignable to type 'A':
|
||||
!!! error TS2322: Type 'number' is not assignable to type 'A'.
|
||||
1.0: new A(),
|
||||
2.0: new B(),
|
||||
"2.5": new B(),
|
||||
|
||||
46
tests/baselines/reference/propertiesAndIndexers2.errors.txt
Normal file
46
tests/baselines/reference/propertiesAndIndexers2.errors.txt
Normal file
@ -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'.
|
||||
}
|
||||
|
||||
19
tests/baselines/reference/propertiesAndIndexers2.js
Normal file
19
tests/baselines/reference/propertiesAndIndexers2.js
Normal file
@ -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]
|
||||
@ -0,0 +1,69 @@
|
||||
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(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 (8 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'.
|
||||
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.
|
||||
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 "hunter2": string = "not a password"; // No error
|
||||
public "+Infinity": string = "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
|
||||
}
|
||||
|
||||
@ -0,0 +1,84 @@
|
||||
//// [propertiesAndIndexersForNumericNames.ts]
|
||||
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
|
||||
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.
|
||||
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 "hunter2": string = "not a password"; // No error
|
||||
public "+Infinity": string = "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
|
||||
}
|
||||
|
||||
|
||||
//// [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
|
||||
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
|
||||
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["hunter2"] = "not a password"; // No error
|
||||
this["+Infinity"] = "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;
|
||||
})();
|
||||
15
tests/cases/compiler/propertiesAndIndexers2.ts
Normal file
15
tests/cases/compiler/propertiesAndIndexers2.ts
Normal file
@ -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;
|
||||
}
|
||||
41
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts
Normal file
41
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts
Normal file
@ -0,0 +1,41 @@
|
||||
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
|
||||
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.
|
||||
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 "hunter2": string = "not a password"; // No error
|
||||
public "+Infinity": string = "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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user