Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use type parameter constraints for computed property types #17404

Merged
merged 10 commits into from
Aug 8, 2017
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13330,10 +13330,13 @@ namespace ts {
const links = getNodeLinks(node.expression);
if (!links.resolvedType) {
links.resolvedType = checkExpression(node.expression);
const t = links.resolvedType.flags & TypeFlags.TypeVariable ?
getBaseConstraintOfType(links.resolvedType) || emptyObjectType :
links.resolvedType;

// This will allow types number, string, symbol or any. It will also allow enums, the unknown
// type, and any union of these types (like string | number).
if (!isTypeAnyOrAllConstituentTypesHaveKind(links.resolvedType, TypeFlags.NumberLike | TypeFlags.StringLike | TypeFlags.ESSymbol)) {
if (!isTypeAnyOrAllConstituentTypesHaveKind(t, TypeFlags.NumberLike | TypeFlags.StringLike | TypeFlags.ESSymbol)) {
error(node, Diagnostics.A_computed_property_name_must_be_of_type_string_number_symbol_or_any);
}
else {
Expand Down
15 changes: 15 additions & 0 deletions tests/baselines/reference/computedPropertyNames51_ES5.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests/cases/conformance/es6/computedProperties/computedPropertyNames51_ES5.ts(5,9): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.


==== tests/cases/conformance/es6/computedProperties/computedPropertyNames51_ES5.ts (1 errors) ====
function f<T, K extends keyof T>() {
var t: T;
var k: K;
var v = {
[t]: 0,
~~~
!!! error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
[k]: 1
};
}

21 changes: 21 additions & 0 deletions tests/baselines/reference/computedPropertyNames51_ES5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [computedPropertyNames51_ES5.ts]
function f<T, K extends keyof T>() {
var t: T;
var k: K;
var v = {
[t]: 0,
[k]: 1
};
}


//// [computedPropertyNames51_ES5.js]
function f() {
var t;
var k;
var v = (_a = {},
_a[t] = 0,
_a[k] = 1,
_a);
var _a;
}
15 changes: 15 additions & 0 deletions tests/baselines/reference/computedPropertyNames51_ES6.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests/cases/conformance/es6/computedProperties/computedPropertyNames51_ES6.ts(5,9): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.


==== tests/cases/conformance/es6/computedProperties/computedPropertyNames51_ES6.ts (1 errors) ====
function f<T, K extends keyof T>() {
var t: T;
var k: K;
var v = {
[t]: 0,
~~~
!!! error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
[k]: 1
};
}

20 changes: 20 additions & 0 deletions tests/baselines/reference/computedPropertyNames51_ES6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [computedPropertyNames51_ES6.ts]
function f<T, K extends keyof T>() {
var t: T;
var k: K;
var v = {
[t]: 0,
[k]: 1
};
}


//// [computedPropertyNames51_ES6.js]
function f() {
var t;
var k;
var v = {
[t]: 0,
[k]: 1
};
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
tests/cases/conformance/es6/computedProperties/computedPropertyNames8_ES5.ts(5,9): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
tests/cases/conformance/es6/computedProperties/computedPropertyNames8_ES5.ts(6,9): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.


==== tests/cases/conformance/es6/computedProperties/computedPropertyNames8_ES5.ts (2 errors) ====
==== tests/cases/conformance/es6/computedProperties/computedPropertyNames8_ES5.ts (1 errors) ====
function f<T, U extends string>() {
var t: T;
var u: U;
Expand All @@ -11,7 +10,5 @@ tests/cases/conformance/es6/computedProperties/computedPropertyNames8_ES5.ts(6,9
~~~
!!! error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
[u]: 1
~~~
!!! error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
};
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
tests/cases/conformance/es6/computedProperties/computedPropertyNames8_ES6.ts(5,9): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
tests/cases/conformance/es6/computedProperties/computedPropertyNames8_ES6.ts(6,9): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.


==== tests/cases/conformance/es6/computedProperties/computedPropertyNames8_ES6.ts (2 errors) ====
==== tests/cases/conformance/es6/computedProperties/computedPropertyNames8_ES6.ts (1 errors) ====
function f<T, U extends string>() {
var t: T;
var u: U;
Expand All @@ -11,7 +10,5 @@ tests/cases/conformance/es6/computedProperties/computedPropertyNames8_ES6.ts(6,9
~~~
!!! error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
[u]: 1
~~~
!!! error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function f<T, K extends keyof T>() {
var t: T;
var k: K;
var v = {
[t]: 0,
[k]: 1
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @target: es6
function f<T, K extends keyof T>() {
var t: T;
var k: K;
var v = {
[t]: 0,
[k]: 1
};
}