mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Use isTypeOfKind in computed property checks
This commit is contained in:
@@ -5383,7 +5383,9 @@ module ts {
|
||||
}
|
||||
|
||||
function isNumericComputedName(name: ComputedPropertyName): boolean {
|
||||
return !!(checkExpression(name.expression).flags & TypeFlags.Number);
|
||||
// It seems odd to consider an expression of type Any to result in a numeric name,
|
||||
// but this behavior is consistent with checkIndexedAccess
|
||||
return isTypeOfKind(checkExpression(name.expression), TypeFlags.Any | TypeFlags.NumberLike);
|
||||
}
|
||||
|
||||
function isNumericLiteralName(name: string) {
|
||||
@@ -5419,7 +5421,7 @@ module ts {
|
||||
// This will only allow types number, string, or any. Any types more complex will
|
||||
// be disallowed, even union types like string | number. In the future, we might consider
|
||||
// allowing types like that.
|
||||
if ((links.resolvedType.flags & (TypeFlags.Number | TypeFlags.String | TypeFlags.Any)) === 0) {
|
||||
if (!isTypeOfKind(links.resolvedType, TypeFlags.Any | TypeFlags.NumberLike | TypeFlags.StringLike)) {
|
||||
error(node, Diagnostics.A_computed_property_name_must_be_of_type_string_number_or_any);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
tests/cases/conformance/es6/computedProperties/computedPropertyNames15.ts(5,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
tests/cases/conformance/es6/computedProperties/computedPropertyNames15.ts(6,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
tests/cases/conformance/es6/computedProperties/computedPropertyNames15.ts(7,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
|
||||
|
||||
==== tests/cases/conformance/es6/computedProperties/computedPropertyNames15.ts (3 errors) ====
|
||||
==== tests/cases/conformance/es6/computedProperties/computedPropertyNames15.ts (2 errors) ====
|
||||
var p1: number | string;
|
||||
var p2: number | number[];
|
||||
var p3: string | boolean;
|
||||
class C {
|
||||
[p1]() { }
|
||||
~~~~
|
||||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
[p2]() { }
|
||||
~~~~
|
||||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
//// [computedPropertyNames46.ts]
|
||||
var o = {
|
||||
["" || 0]: 0
|
||||
};
|
||||
|
||||
//// [computedPropertyNames46.js]
|
||||
var o = {
|
||||
["" || 0]: 0
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
=== tests/cases/conformance/es6/computedProperties/computedPropertyNames46.ts ===
|
||||
var o = {
|
||||
>o : {}
|
||||
>{ ["" || 0]: 0} : {}
|
||||
|
||||
["" || 0]: 0
|
||||
>"" || 0 : string | number
|
||||
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
//// [computedPropertyNames47.ts]
|
||||
enum E1 { x }
|
||||
enum E2 { x }
|
||||
var o = {
|
||||
[E1.x || E2.x]: 0
|
||||
};
|
||||
|
||||
//// [computedPropertyNames47.js]
|
||||
var E1;
|
||||
(function (E1) {
|
||||
E1[E1["x"] = 0] = "x";
|
||||
})(E1 || (E1 = {}));
|
||||
var E2;
|
||||
(function (E2) {
|
||||
E2[E2["x"] = 0] = "x";
|
||||
})(E2 || (E2 = {}));
|
||||
var o = {
|
||||
[0 /* x */ || 0 /* x */]: 0
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
=== tests/cases/conformance/es6/computedProperties/computedPropertyNames47.ts ===
|
||||
enum E1 { x }
|
||||
>E1 : E1
|
||||
>x : E1
|
||||
|
||||
enum E2 { x }
|
||||
>E2 : E2
|
||||
>x : E2
|
||||
|
||||
var o = {
|
||||
>o : {}
|
||||
>{ [E1.x || E2.x]: 0} : {}
|
||||
|
||||
[E1.x || E2.x]: 0
|
||||
>E1.x || E2.x : E1 | E2
|
||||
>E1.x : E1
|
||||
>E1 : typeof E1
|
||||
>x : E1
|
||||
>E2.x : E2
|
||||
>E2 : typeof E2
|
||||
>x : E2
|
||||
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
//// [computedPropertyNames48.ts]
|
||||
declare function extractIndexer<T>(p: { [n: number]: T }): T;
|
||||
|
||||
enum E { x }
|
||||
|
||||
var a: any;
|
||||
|
||||
extractIndexer({
|
||||
[a]: ""
|
||||
}); // Should return string
|
||||
|
||||
extractIndexer({
|
||||
[E.x]: ""
|
||||
}); // Should return string
|
||||
|
||||
extractIndexer({
|
||||
["" || 0]: ""
|
||||
}); // Should return any (widened form of undefined)
|
||||
|
||||
//// [computedPropertyNames48.js]
|
||||
var E;
|
||||
(function (E) {
|
||||
E[E["x"] = 0] = "x";
|
||||
})(E || (E = {}));
|
||||
var a;
|
||||
extractIndexer({
|
||||
[a]: ""
|
||||
}); // Should return string
|
||||
extractIndexer({
|
||||
[0 /* x */]: ""
|
||||
}); // Should return string
|
||||
extractIndexer({
|
||||
["" || 0]: ""
|
||||
}); // Should return any (widened form of undefined)
|
||||
@@ -0,0 +1,47 @@
|
||||
=== tests/cases/conformance/es6/computedProperties/computedPropertyNames48.ts ===
|
||||
declare function extractIndexer<T>(p: { [n: number]: T }): T;
|
||||
>extractIndexer : <T>(p: { [n: number]: T; }) => T
|
||||
>T : T
|
||||
>p : { [n: number]: T; }
|
||||
>n : number
|
||||
>T : T
|
||||
>T : T
|
||||
|
||||
enum E { x }
|
||||
>E : E
|
||||
>x : E
|
||||
|
||||
var a: any;
|
||||
>a : any
|
||||
|
||||
extractIndexer({
|
||||
>extractIndexer({ [a]: ""}) : string
|
||||
>extractIndexer : <T>(p: { [n: number]: T; }) => T
|
||||
>{ [a]: ""} : { [x: number]: string; }
|
||||
|
||||
[a]: ""
|
||||
>a : any
|
||||
|
||||
}); // Should return string
|
||||
|
||||
extractIndexer({
|
||||
>extractIndexer({ [E.x]: ""}) : string
|
||||
>extractIndexer : <T>(p: { [n: number]: T; }) => T
|
||||
>{ [E.x]: ""} : { [x: number]: string; }
|
||||
|
||||
[E.x]: ""
|
||||
>E.x : E
|
||||
>E : typeof E
|
||||
>x : E
|
||||
|
||||
}); // Should return string
|
||||
|
||||
extractIndexer({
|
||||
>extractIndexer({ ["" || 0]: ""}) : any
|
||||
>extractIndexer : <T>(p: { [n: number]: T; }) => T
|
||||
>{ ["" || 0]: ""} : { [x: number]: undefined; }
|
||||
|
||||
["" || 0]: ""
|
||||
>"" || 0 : string | number
|
||||
|
||||
}); // Should return any (widened form of undefined)
|
||||
@@ -1,16 +1,13 @@
|
||||
tests/cases/conformance/es6/computedProperties/computedPropertyNames6.ts(5,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
tests/cases/conformance/es6/computedProperties/computedPropertyNames6.ts(6,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
tests/cases/conformance/es6/computedProperties/computedPropertyNames6.ts(7,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
|
||||
|
||||
==== tests/cases/conformance/es6/computedProperties/computedPropertyNames6.ts (3 errors) ====
|
||||
==== tests/cases/conformance/es6/computedProperties/computedPropertyNames6.ts (2 errors) ====
|
||||
var p1: number | string;
|
||||
var p2: number | number[];
|
||||
var p3: string | boolean;
|
||||
var v = {
|
||||
[p1]: 0,
|
||||
~~~~
|
||||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
[p2]: 1,
|
||||
~~~~
|
||||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
tests/cases/conformance/es6/computedProperties/computedPropertyNames7.ts(5,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
|
||||
|
||||
==== tests/cases/conformance/es6/computedProperties/computedPropertyNames7.ts (1 errors) ====
|
||||
enum E {
|
||||
member
|
||||
}
|
||||
var v = {
|
||||
[E.member]: 0
|
||||
~~~~~~~~~~
|
||||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
=== tests/cases/conformance/es6/computedProperties/computedPropertyNames7.ts ===
|
||||
enum E {
|
||||
>E : E
|
||||
|
||||
member
|
||||
>member : E
|
||||
}
|
||||
var v = {
|
||||
>v : {}
|
||||
>{ [E.member]: 0} : {}
|
||||
|
||||
[E.member]: 0
|
||||
>E.member : E
|
||||
>E : typeof E
|
||||
>member : E
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// @target: es6
|
||||
var o = {
|
||||
["" || 0]: 0
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
// @target: es6
|
||||
enum E1 { x }
|
||||
enum E2 { x }
|
||||
var o = {
|
||||
[E1.x || E2.x]: 0
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
// @target: es6
|
||||
declare function extractIndexer<T>(p: { [n: number]: T }): T;
|
||||
|
||||
enum E { x }
|
||||
|
||||
var a: any;
|
||||
|
||||
extractIndexer({
|
||||
[a]: ""
|
||||
}); // Should return string
|
||||
|
||||
extractIndexer({
|
||||
[E.x]: ""
|
||||
}); // Should return string
|
||||
|
||||
extractIndexer({
|
||||
["" || 0]: ""
|
||||
}); // Should return any (widened form of undefined)
|
||||
Reference in New Issue
Block a user