Skip to content

Computed property names are const contexts #46464

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33488,6 +33488,7 @@ namespace ts {
const parent = node.parent;
return isAssertionExpression(parent) && isConstTypeReference(parent.type) ||
isJSDocTypeAssertion(parent) && isConstTypeReference(getJSDocTypeAssertionType(parent)) ||
isComputedPropertyName(parent) ||
(isParenthesizedExpression(parent) || isArrayLiteralExpression(parent) || isSpreadElement(parent)) && isConstContext(parent) ||
(isPropertyAssignment(parent) || isShorthandPropertyAssignment(parent) || isTemplateSpan(parent)) && isConstContext(parent.parent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ var v = {

[`hello ${a} bye`]() { }
>[`hello ${a} bye`] : () => void
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ var v = {

[`hello ${a} bye`]() { }
>[`hello ${a} bye`] : () => void
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var v = {

get [`hello ${a} bye`]() { return 0; }
>[`hello ${a} bye`] : number
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
>0 : 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var v = {

get [`hello ${a} bye`]() { return 0; }
>[`hello ${a} bye`] : number
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
>0 : 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class C {

static [`hello ${a} bye`] = 0
>[`hello ${a} bye`] : number
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
>0 : 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class C {

static [`hello ${a} bye`] = 0
>[`hello ${a} bye`] : number
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
>0 : 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ class C {

static [`hello ${a} bye`]() { }
>[`hello ${a} bye`] : () => void
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ class C {

static [`hello ${a} bye`]() { }
>[`hello ${a} bye`] : () => void
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class C {

[[]]() { }
>[[]] : () => void
>[] : undefined[]
>[] : readonly []

static [{}]() { }
>[{}] : () => void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class C {

[[]]() { }
>[[]] : () => void
>[] : undefined[]
>[] : readonly []

static [{}]() { }
>[{}] : () => void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class C {

get [`hello ${a} bye`]() { return 0; }
>[`hello ${a} bye`] : number
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
>0 : 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class C {

get [`hello ${a} bye`]() { return 0; }
>[`hello ${a} bye`] : number
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
>0 : 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class C {

get [[]]() { return 0; }
>[[]] : number
>[] : undefined[]
>[] : readonly []
>0 : 0

set [{}](v) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class C {

get [[]]() { return 0; }
>[[]] : number
>[] : undefined[]
>[] : readonly []
>0 : 0

set [{}](v) { }
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/computedPropertyNames3_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class C {

set [[0, 1]](v) { }
>[[0, 1]] : any
>[0, 1] : number[]
>[0, 1] : readonly [0, 1]
>0 : 0
>1 : 1
>v : any
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/computedPropertyNames3_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class C {

set [[0, 1]](v) { }
>[[0, 1]] : any
>[0, 1] : number[]
>[0, 1] : readonly [0, 1]
>0 : 0
>1 : 1
>v : any
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/computedPropertyNames4_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var v = {

[`hello ${a} bye`]: 0
>[`hello ${a} bye`] : number
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
>0 : 0
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/computedPropertyNames4_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var v = {

[`hello ${a} bye`]: 0
>[`hello ${a} bye`] : number
>`hello ${a} bye` : string
>`hello ${a} bye` : `hello ${any} bye`
>a : any
>0 : 0
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/computedPropertyNames5_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var v = {

[[]]: 0,
>[[]] : number
>[] : undefined[]
>[] : readonly []
>0 : 0

[{}]: 0,
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/computedPropertyNames5_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var v = {

[[]]: 0,
>[[]] : number
>[] : undefined[]
>[] : readonly []
>0 : 0

[{}]: 0,
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/constContextTemplateLiteral.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tests/cases/conformance/expressions/literals/constContextTemplateLiteral.ts(3,24): error TS2418: Type of computed property's value is '{ b: string; }', which is not assignable to type '{ a: any; }'.
Object literal may only specify known properties, and 'b' does not exist in type '{ a: any; }'.


==== tests/cases/conformance/expressions/literals/constContextTemplateLiteral.ts (1 errors) ====
type Person = { id: number }
const persons: Record<string, { a: any }> = {
[`person-${1}`]: { b: "something" }, // ok, error
~~~~~~~~~~~~~~
!!! error TS2418: Type of computed property's value is '{ b: string; }', which is not assignable to type '{ a: any; }'.
!!! error TS2418: Object literal may only specify known properties, and 'b' does not exist in type '{ a: any; }'.
}

12 changes: 12 additions & 0 deletions tests/baselines/reference/constContextTemplateLiteral.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [constContextTemplateLiteral.ts]
type Person = { id: number }
const persons: Record<string, { a: any }> = {
[`person-${1}`]: { b: "something" }, // ok, error
}


//// [constContextTemplateLiteral.js]
var _a;
var persons = (_a = {},
_a["person-".concat(1)] = { b: "something" },
_a);
15 changes: 15 additions & 0 deletions tests/baselines/reference/constContextTemplateLiteral.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== tests/cases/conformance/expressions/literals/constContextTemplateLiteral.ts ===
type Person = { id: number }
>Person : Symbol(Person, Decl(constContextTemplateLiteral.ts, 0, 0))
>id : Symbol(id, Decl(constContextTemplateLiteral.ts, 0, 15))

const persons: Record<string, { a: any }> = {
>persons : Symbol(persons, Decl(constContextTemplateLiteral.ts, 1, 5))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>a : Symbol(a, Decl(constContextTemplateLiteral.ts, 1, 31))

[`person-${1}`]: { b: "something" }, // ok, error
>[`person-${1}`] : Symbol([`person-${1}`], Decl(constContextTemplateLiteral.ts, 1, 45))
>b : Symbol(b, Decl(constContextTemplateLiteral.ts, 2, 22))
}

19 changes: 19 additions & 0 deletions tests/baselines/reference/constContextTemplateLiteral.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/conformance/expressions/literals/constContextTemplateLiteral.ts ===
type Person = { id: number }
>Person : Person
>id : number

const persons: Record<string, { a: any }> = {
>persons : Record<string, { a: any; }>
>a : any
>{ [`person-${1}`]: { b: "something" }, // ok, error} : { "person-1": { b: string; }; }

[`person-${1}`]: { b: "something" }, // ok, error
>[`person-${1}`] : { b: string; }
>`person-${1}` : "person-1"
>1 : 1
>{ b: "something" } : { b: string; }
>b : string
>"something" : "something"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Person = { id: number }
const persons: Record<string, { a: any }> = {
[`person-${1}`]: { b: "something" }, // ok, error
}