Skip to content

Commit

Permalink
Correctly check computed property names in type-space get/set accesso…
Browse files Browse the repository at this point in the history
…rs (microsoft#47156)

* Add test that should fail

* Make it fail

Fixes microsoft#47146

* Baselines
  • Loading branch information
RyanCavanaugh authored Feb 1, 2022
1 parent 46e7ab4 commit 2172e19
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27089,7 +27089,8 @@ namespace ts {
const links = getNodeLinks(node.expression);
if (!links.resolvedType) {
if ((isTypeLiteralNode(node.parent.parent) || isClassLike(node.parent.parent) || isInterfaceDeclaration(node.parent.parent))
&& isBinaryExpression(node.expression) && node.expression.operatorToken.kind === SyntaxKind.InKeyword) {
&& isBinaryExpression(node.expression) && node.expression.operatorToken.kind === SyntaxKind.InKeyword
&& node.parent.kind !== SyntaxKind.GetAccessor && node.parent.kind !== SyntaxKind.SetAccessor) {
return links.resolvedType = errorType;
}
links.resolvedType = checkExpression(node.expression);
Expand Down
16 changes: 16 additions & 0 deletions tests/baselines/reference/noMappedGetSet.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tests/cases/compiler/noMappedGetSet.ts(2,9): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
tests/cases/compiler/noMappedGetSet.ts(2,10): error TS2304: Cannot find name 'K'.
tests/cases/compiler/noMappedGetSet.ts(2,15): error TS2304: Cannot find name 'WAT'.


==== tests/cases/compiler/noMappedGetSet.ts (3 errors) ====
type OH_NO = {
get [K in WAT](): string
~~~~~~~~~~
!!! error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
~
!!! error TS2304: Cannot find name 'K'.
~~~
!!! error TS2304: Cannot find name 'WAT'.
};

7 changes: 7 additions & 0 deletions tests/baselines/reference/noMappedGetSet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//// [noMappedGetSet.ts]
type OH_NO = {
get [K in WAT](): string
};


//// [noMappedGetSet.js]
9 changes: 9 additions & 0 deletions tests/baselines/reference/noMappedGetSet.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/compiler/noMappedGetSet.ts ===
type OH_NO = {
>OH_NO : Symbol(OH_NO, Decl(noMappedGetSet.ts, 0, 0))

get [K in WAT](): string
>[K in WAT] : Symbol([K in WAT], Decl(noMappedGetSet.ts, 0, 14))

};

12 changes: 12 additions & 0 deletions tests/baselines/reference/noMappedGetSet.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/compiler/noMappedGetSet.ts ===
type OH_NO = {
>OH_NO : OH_NO

get [K in WAT](): string
>[K in WAT] : string
>K in WAT : boolean
>K : any
>WAT : any

};

3 changes: 3 additions & 0 deletions tests/cases/compiler/noMappedGetSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type OH_NO = {
get [K in WAT](): string
};

0 comments on commit 2172e19

Please sign in to comment.