Skip to content

Commit 95a42cf

Browse files
authored
Fix auto-accessor type resolution (#4184)
1 parent 40f6707 commit 95a42cf

6 files changed

Lines changed: 83 additions & 5 deletions

File tree

internal/checker/checker.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18377,11 +18377,7 @@ func (c *Checker) getTypeOfAccessors(symbol *ast.Symbol) *Type {
1837718377
}
1837818378
getter := ast.GetDeclarationOfKind(symbol, ast.KindGetAccessor)
1837918379
setter := ast.GetDeclarationOfKind(symbol, ast.KindSetAccessor)
18380-
property := ast.GetDeclarationOfKind(symbol, ast.KindPropertyDeclaration)
18381-
var accessor *ast.Node
18382-
if property != nil && ast.IsAutoAccessorPropertyDeclaration(property) {
18383-
accessor = property
18384-
}
18380+
accessor := core.Find(symbol.Declarations, ast.IsAutoAccessorPropertyDeclaration)
1838518381
// We try to resolve a getter type annotation, a setter type annotation, or a getter function
1838618382
// body return type inference, in that order.
1838718383
t := c.getAnnotatedAccessorType(getter)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
duplicatePropertyAndAccessor.ts(4,3): error TS2300: Duplicate identifier 'y'.
2+
duplicatePropertyAndAccessor.ts(5,12): error TS2300: Duplicate identifier 'y'.
3+
4+
5+
==== duplicatePropertyAndAccessor.ts (2 errors) ====
6+
// https://github.com/microsoft/typescript-go/issues/4130
7+
8+
class C {
9+
y: number = 2;
10+
~
11+
!!! error TS2300: Duplicate identifier 'y'.
12+
accessor y: number = 3;
13+
~
14+
!!! error TS2300: Duplicate identifier 'y'.
15+
}
16+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [tests/cases/compiler/duplicatePropertyAndAccessor.ts] ////
2+
3+
//// [duplicatePropertyAndAccessor.ts]
4+
// https://github.com/microsoft/typescript-go/issues/4130
5+
6+
class C {
7+
y: number = 2;
8+
accessor y: number = 3;
9+
}
10+
11+
12+
//// [duplicatePropertyAndAccessor.js]
13+
"use strict";
14+
// https://github.com/microsoft/typescript-go/issues/4130
15+
class C {
16+
y = 2;
17+
accessor y = 3;
18+
}
19+
20+
21+
//// [duplicatePropertyAndAccessor.d.ts]
22+
declare class C {
23+
y: number;
24+
accessor y: number;
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/compiler/duplicatePropertyAndAccessor.ts] ////
2+
3+
=== duplicatePropertyAndAccessor.ts ===
4+
// https://github.com/microsoft/typescript-go/issues/4130
5+
6+
class C {
7+
>C : Symbol(C, Decl(duplicatePropertyAndAccessor.ts, 0, 0))
8+
9+
y: number = 2;
10+
>y : Symbol(C.y, Decl(duplicatePropertyAndAccessor.ts, 2, 9), Decl(duplicatePropertyAndAccessor.ts, 3, 16))
11+
12+
accessor y: number = 3;
13+
>y : Symbol(C.y, Decl(duplicatePropertyAndAccessor.ts, 2, 9), Decl(duplicatePropertyAndAccessor.ts, 3, 16))
14+
}
15+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/duplicatePropertyAndAccessor.ts] ////
2+
3+
=== duplicatePropertyAndAccessor.ts ===
4+
// https://github.com/microsoft/typescript-go/issues/4130
5+
6+
class C {
7+
>C : C
8+
9+
y: number = 2;
10+
>y : number
11+
>2 : 2
12+
13+
accessor y: number = 3;
14+
>y : number
15+
>3 : 3
16+
}
17+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @declaration: true
2+
// @target: esnext
3+
4+
// https://github.com/microsoft/typescript-go/issues/4130
5+
6+
class C {
7+
y: number = 2;
8+
accessor y: number = 3;
9+
}

0 commit comments

Comments
 (0)