Skip to content

Commit db429aa

Browse files
committed
correct auto container lookup
1 parent 3e4b06f commit db429aa

9 files changed

+372
-56
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11366,14 +11366,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1136611366
if (!isThisProperty(node) || !isAutoTypedProperty(prop)) {
1136711367
return false;
1136811368
}
11369-
const thisContainer = getThisContainer(node, /*includeArrowFunctions*/ false, /*includeClassComputedPropertyName*/ false);
11370-
return isClassStaticBlockDeclaration(thisContainer) && (prop.parent && getClassLikeDeclarationOfSymbol(prop.parent)) === thisContainer.parent;
11369+
const autoContainer = getAutoContainer(node);
11370+
return isClassStaticBlockDeclaration(autoContainer) && (prop.parent && getClassLikeDeclarationOfSymbol(prop.parent)) === autoContainer.parent;
1137111371
}
1137211372
if (!isConstructorDeclaredProperty(prop) && (!isThisProperty(node) || !isAutoTypedProperty(prop))) {
1137311373
return false;
1137411374
}
11375-
const thisContainer = getThisContainer(node, /*includeArrowFunctions*/ false, /*includeClassComputedPropertyName*/ false);
11376-
return isConstructorDeclaration(thisContainer) && (prop.parent && getClassLikeDeclarationOfSymbol(prop.parent)) === thisContainer.parent || thisContainer === getDeclaringConstructor(prop);
11375+
const autoContainer = getAutoContainer(node);
11376+
return isConstructorDeclaration(autoContainer) && (prop.parent && getClassLikeDeclarationOfSymbol(prop.parent)) === autoContainer.parent || autoContainer === getDeclaringConstructor(prop);
11377+
}
11378+
11379+
function getAutoContainer(node: Node) {
11380+
return findAncestor(node.parent, node => !!getContainingFunctionOrClassStaticBlock(node) && !getImmediatelyInvokedFunctionExpression(node))!;
1137711381
}
1137811382

1137911383
function getDeclaringConstructor(symbol: Symbol) {

tests/baselines/reference/classAttributeInferenceContextualTypingOutsideOfConstructor1.errors.txt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
classAttributeInferenceContextualTypingOutsideOfConstructor1.ts(16,20): error TS2820: Type '"runnnning"' is not assignable to type '"running" | "stopped"'. Did you mean '"running"'?
2-
classAttributeInferenceContextualTypingOutsideOfConstructor1.ts(29,20): error TS2820: Type '"runnnning"' is not assignable to type '"running" | "stopped"'. Did you mean '"running"'?
1+
classAttributeInferenceContextualTypingOutsideOfConstructor1.ts(15,22): error TS2820: Type '"runnnning"' is not assignable to type '"running" | "stopped"'. Did you mean '"running"'?
2+
classAttributeInferenceContextualTypingOutsideOfConstructor1.ts(21,20): error TS2820: Type '"runnnning"' is not assignable to type '"running" | "stopped"'. Did you mean '"running"'?
3+
classAttributeInferenceContextualTypingOutsideOfConstructor1.ts(33,22): error TS2820: Type '"runnnning"' is not assignable to type '"running" | "stopped"'. Did you mean '"running"'?
4+
classAttributeInferenceContextualTypingOutsideOfConstructor1.ts(39,20): error TS2820: Type '"runnnning"' is not assignable to type '"running" | "stopped"'. Did you mean '"running"'?
35

46

5-
==== classAttributeInferenceContextualTypingOutsideOfConstructor1.ts (2 errors) ====
7+
==== classAttributeInferenceContextualTypingOutsideOfConstructor1.ts (4 errors) ====
68
// https://github.com/microsoft/TypeScript/issues/60394
79

810
type State = { type: "running"; speed: number } | { type: "stopped" };
@@ -14,6 +16,14 @@ classAttributeInferenceContextualTypingOutsideOfConstructor1.ts(29,20): error TS
1416

1517
constructor() {
1618
this.state = initialState;
19+
20+
const localRun = (speed: number) => {
21+
this.state = { type: "running", speed };
22+
this.state = { type: "runnnning", speed }; // error
23+
~~~~
24+
!!! error TS2820: Type '"runnnning"' is not assignable to type '"running" | "stopped"'. Did you mean '"running"'?
25+
!!! related TS6500 classAttributeInferenceContextualTypingOutsideOfConstructor1.ts:3:16: The expected type comes from property 'type' which is declared here on type 'State'
26+
}
1727
}
1828

1929
run(speed: number) {
@@ -30,6 +40,14 @@ classAttributeInferenceContextualTypingOutsideOfConstructor1.ts(29,20): error TS
3040

3141
constructor() {
3242
this.state = initialState;
43+
44+
const localRun = (speed: number) => {
45+
this.state = { type: "running", speed };
46+
this.state = { type: "runnnning", speed }; // error
47+
~~~~
48+
!!! error TS2820: Type '"runnnning"' is not assignable to type '"running" | "stopped"'. Did you mean '"running"'?
49+
!!! related TS6500 classAttributeInferenceContextualTypingOutsideOfConstructor1.ts:3:16: The expected type comes from property 'type' which is declared here on type 'State'
50+
}
3351
}
3452

3553
run(speed: number) {

tests/baselines/reference/classAttributeInferenceContextualTypingOutsideOfConstructor1.symbols

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,59 +25,97 @@ class Actor1 {
2525
>this : Symbol(Actor1, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 4, 34))
2626
>state : Symbol(Actor1.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 6, 14))
2727
>initialState : Symbol(initialState, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 4, 13))
28+
29+
const localRun = (speed: number) => {
30+
>localRun : Symbol(localRun, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 12, 9))
31+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 12, 22))
32+
33+
this.state = { type: "running", speed };
34+
>this.state : Symbol(Actor1.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 6, 14))
35+
>this : Symbol(Actor1, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 4, 34))
36+
>state : Symbol(Actor1.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 6, 14))
37+
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 13, 20))
38+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 13, 37))
39+
40+
this.state = { type: "runnnning", speed }; // error
41+
>this.state : Symbol(Actor1.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 6, 14))
42+
>this : Symbol(Actor1, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 4, 34))
43+
>state : Symbol(Actor1.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 6, 14))
44+
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 14, 20))
45+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 14, 39))
46+
}
2847
}
2948

3049
run(speed: number) {
31-
>run : Symbol(Actor1.run, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 11, 3))
32-
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 13, 6))
50+
>run : Symbol(Actor1.run, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 16, 3))
51+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 18, 6))
3352

3453
this.state = { type: "running", speed };
3554
>this.state : Symbol(Actor1.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 6, 14))
3655
>this : Symbol(Actor1, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 4, 34))
3756
>state : Symbol(Actor1.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 6, 14))
38-
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 14, 18))
39-
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 14, 35))
57+
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 19, 18))
58+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 19, 35))
4059

4160
this.state = { type: "runnnning", speed }; // error
4261
>this.state : Symbol(Actor1.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 6, 14))
4362
>this : Symbol(Actor1, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 4, 34))
4463
>state : Symbol(Actor1.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 6, 14))
45-
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 15, 18))
46-
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 15, 37))
64+
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 20, 18))
65+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 20, 37))
4766
}
4867
}
4968

5069
class Actor2 {
51-
>Actor2 : Symbol(Actor2, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 17, 1))
70+
>Actor2 : Symbol(Actor2, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 22, 1))
5271

5372
accessor state;
54-
>state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 19, 14))
73+
>state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 14))
5574

5675
constructor() {
5776
this.state = initialState;
58-
>this.state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 19, 14))
59-
>this : Symbol(Actor2, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 17, 1))
60-
>state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 19, 14))
77+
>this.state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 14))
78+
>this : Symbol(Actor2, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 22, 1))
79+
>state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 14))
6180
>initialState : Symbol(initialState, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 4, 13))
81+
82+
const localRun = (speed: number) => {
83+
>localRun : Symbol(localRun, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 30, 9))
84+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 30, 22))
85+
86+
this.state = { type: "running", speed };
87+
>this.state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 14))
88+
>this : Symbol(Actor2, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 22, 1))
89+
>state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 14))
90+
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 31, 20))
91+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 31, 37))
92+
93+
this.state = { type: "runnnning", speed }; // error
94+
>this.state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 14))
95+
>this : Symbol(Actor2, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 22, 1))
96+
>state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 14))
97+
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 32, 20))
98+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 32, 39))
99+
}
62100
}
63101

64102
run(speed: number) {
65-
>run : Symbol(Actor2.run, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 3))
66-
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 26, 6))
103+
>run : Symbol(Actor2.run, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 34, 3))
104+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 36, 6))
67105

68106
this.state = { type: "running", speed };
69-
>this.state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 19, 14))
70-
>this : Symbol(Actor2, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 17, 1))
71-
>state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 19, 14))
72-
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 27, 18))
73-
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 27, 35))
107+
>this.state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 14))
108+
>this : Symbol(Actor2, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 22, 1))
109+
>state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 14))
110+
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 37, 18))
111+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 37, 35))
74112

75113
this.state = { type: "runnnning", speed }; // error
76-
>this.state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 19, 14))
77-
>this : Symbol(Actor2, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 17, 1))
78-
>state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 19, 14))
79-
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 28, 18))
80-
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 28, 37))
114+
>this.state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 14))
115+
>this : Symbol(Actor2, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 22, 1))
116+
>state : Symbol(Actor2.state, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 24, 14))
117+
>type : Symbol(type, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 38, 18))
118+
>speed : Symbol(speed, Decl(classAttributeInferenceContextualTypingOutsideOfConstructor1.ts, 38, 37))
81119
}
82120
}
83121

tests/baselines/reference/classAttributeInferenceContextualTypingOutsideOfConstructor1.types

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,51 @@ class Actor1 {
3737
> : ^^^^^
3838
>initialState : State
3939
> : ^^^^^
40+
41+
const localRun = (speed: number) => {
42+
>localRun : (speed: number) => void
43+
> : ^ ^^ ^^^^^^^^^
44+
>(speed: number) => { this.state = { type: "running", speed }; this.state = { type: "runnnning", speed }; // error } : (speed: number) => void
45+
> : ^ ^^ ^^^^^^^^^
46+
>speed : number
47+
> : ^^^^^^
48+
49+
this.state = { type: "running", speed };
50+
>this.state = { type: "running", speed } : { type: "running"; speed: number; }
51+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
>this.state : State
53+
> : ^^^^^
54+
>this : this
55+
> : ^^^^
56+
>state : State
57+
> : ^^^^^
58+
>{ type: "running", speed } : { type: "running"; speed: number; }
59+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
60+
>type : "running"
61+
> : ^^^^^^^^^
62+
>"running" : "running"
63+
> : ^^^^^^^^^
64+
>speed : number
65+
> : ^^^^^^
66+
67+
this.state = { type: "runnnning", speed }; // error
68+
>this.state = { type: "runnnning", speed } : { type: "runnnning"; speed: number; }
69+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
70+
>this.state : State
71+
> : ^^^^^
72+
>this : this
73+
> : ^^^^
74+
>state : State
75+
> : ^^^^^
76+
>{ type: "runnnning", speed } : { type: "runnnning"; speed: number; }
77+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
78+
>type : "runnnning"
79+
> : ^^^^^^^^^^^
80+
>"runnnning" : "runnnning"
81+
> : ^^^^^^^^^^^
82+
>speed : number
83+
> : ^^^^^^
84+
}
4085
}
4186

4287
run(speed: number) {
@@ -103,6 +148,51 @@ class Actor2 {
103148
> : ^^^^^
104149
>initialState : State
105150
> : ^^^^^
151+
152+
const localRun = (speed: number) => {
153+
>localRun : (speed: number) => void
154+
> : ^ ^^ ^^^^^^^^^
155+
>(speed: number) => { this.state = { type: "running", speed }; this.state = { type: "runnnning", speed }; // error } : (speed: number) => void
156+
> : ^ ^^ ^^^^^^^^^
157+
>speed : number
158+
> : ^^^^^^
159+
160+
this.state = { type: "running", speed };
161+
>this.state = { type: "running", speed } : { type: "running"; speed: number; }
162+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
163+
>this.state : State
164+
> : ^^^^^
165+
>this : this
166+
> : ^^^^
167+
>state : State
168+
> : ^^^^^
169+
>{ type: "running", speed } : { type: "running"; speed: number; }
170+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
171+
>type : "running"
172+
> : ^^^^^^^^^
173+
>"running" : "running"
174+
> : ^^^^^^^^^
175+
>speed : number
176+
> : ^^^^^^
177+
178+
this.state = { type: "runnnning", speed }; // error
179+
>this.state = { type: "runnnning", speed } : { type: "runnnning"; speed: number; }
180+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
181+
>this.state : State
182+
> : ^^^^^
183+
>this : this
184+
> : ^^^^
185+
>state : State
186+
> : ^^^^^
187+
>{ type: "runnnning", speed } : { type: "runnnning"; speed: number; }
188+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
189+
>type : "runnnning"
190+
> : ^^^^^^^^^^^
191+
>"runnnning" : "runnnning"
192+
> : ^^^^^^^^^^^
193+
>speed : number
194+
> : ^^^^^^
195+
}
106196
}
107197

108198
run(speed: number) {

0 commit comments

Comments
 (0)