-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Mark class property initializers as outside of CFA containers #63310
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
Open
Copilot
wants to merge
3
commits into
main
Choose a base branch
from
copilot/fix-narrow-inference-regression
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
tests/baselines/reference/classPropertyInferenceFromBroaderTypeConst.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| //// [tests/cases/compiler/classPropertyInferenceFromBroaderTypeConst.ts] //// | ||
|
|
||
| //// [classPropertyInferenceFromBroaderTypeConst.ts] | ||
| // Repro from GH#62264 | ||
| // Class property should infer the wider declared type (AB), not the narrowed literal type ("A") | ||
|
|
||
| type AB = 'A' | 'B'; | ||
|
|
||
| const DEFAULT: AB = 'A'; | ||
|
|
||
| class C { | ||
| D = DEFAULT; | ||
|
|
||
| method() { | ||
| switch (this.D) { | ||
| case 'A': break; | ||
| case 'B': break; // should not error | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // D should be AB, not "A" | ||
| declare const c: C; | ||
| declare function expectAB(x: AB): void; | ||
| expectAB(c.D); // ok | ||
| c.D = 'B'; // ok | ||
|
|
||
| // Static property should work the same way | ||
| class D { | ||
| static SD = DEFAULT; | ||
| } | ||
| D.SD = 'B'; // ok | ||
|
|
||
|
|
||
| //// [classPropertyInferenceFromBroaderTypeConst.js] | ||
| "use strict"; | ||
| // Repro from GH#62264 | ||
| // Class property should infer the wider declared type (AB), not the narrowed literal type ("A") | ||
| const DEFAULT = 'A'; | ||
| class C { | ||
| D = DEFAULT; | ||
| method() { | ||
| switch (this.D) { | ||
| case 'A': break; | ||
| case 'B': break; // should not error | ||
| } | ||
| } | ||
| } | ||
| expectAB(c.D); // ok | ||
| c.D = 'B'; // ok | ||
| // Static property should work the same way | ||
| class D { | ||
| static SD = DEFAULT; | ||
| } | ||
| D.SD = 'B'; // ok |
68 changes: 68 additions & 0 deletions
68
tests/baselines/reference/classPropertyInferenceFromBroaderTypeConst.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| //// [tests/cases/compiler/classPropertyInferenceFromBroaderTypeConst.ts] //// | ||
|
|
||
| === classPropertyInferenceFromBroaderTypeConst.ts === | ||
| // Repro from GH#62264 | ||
| // Class property should infer the wider declared type (AB), not the narrowed literal type ("A") | ||
|
|
||
| type AB = 'A' | 'B'; | ||
| >AB : Symbol(AB, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 0, 0)) | ||
|
|
||
| const DEFAULT: AB = 'A'; | ||
| >DEFAULT : Symbol(DEFAULT, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 5)) | ||
| >AB : Symbol(AB, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 0, 0)) | ||
|
|
||
| class C { | ||
| >C : Symbol(C, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 24)) | ||
|
|
||
| D = DEFAULT; | ||
| >D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) | ||
| >DEFAULT : Symbol(DEFAULT, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 5)) | ||
|
|
||
| method() { | ||
| >method : Symbol(C.method, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 8, 16)) | ||
|
|
||
| switch (this.D) { | ||
| >this.D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) | ||
| >this : Symbol(C, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 24)) | ||
| >D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) | ||
|
|
||
| case 'A': break; | ||
| case 'B': break; // should not error | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // D should be AB, not "A" | ||
| declare const c: C; | ||
| >c : Symbol(c, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 19, 13)) | ||
| >C : Symbol(C, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 24)) | ||
|
|
||
| declare function expectAB(x: AB): void; | ||
| >expectAB : Symbol(expectAB, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 19, 19)) | ||
| >x : Symbol(x, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 20, 26)) | ||
| >AB : Symbol(AB, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 0, 0)) | ||
|
|
||
| expectAB(c.D); // ok | ||
| >expectAB : Symbol(expectAB, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 19, 19)) | ||
| >c.D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) | ||
| >c : Symbol(c, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 19, 13)) | ||
| >D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) | ||
|
|
||
| c.D = 'B'; // ok | ||
| >c.D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) | ||
| >c : Symbol(c, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 19, 13)) | ||
| >D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) | ||
|
|
||
| // Static property should work the same way | ||
| class D { | ||
| >D : Symbol(D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 22, 10)) | ||
|
|
||
| static SD = DEFAULT; | ||
| >SD : Symbol(D.SD, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 25, 9)) | ||
| >DEFAULT : Symbol(DEFAULT, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 5)) | ||
| } | ||
| D.SD = 'B'; // ok | ||
| >D.SD : Symbol(D.SD, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 25, 9)) | ||
| >D : Symbol(D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 22, 10)) | ||
| >SD : Symbol(D.SD, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 25, 9)) | ||
|
|
107 changes: 107 additions & 0 deletions
107
tests/baselines/reference/classPropertyInferenceFromBroaderTypeConst.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| //// [tests/cases/compiler/classPropertyInferenceFromBroaderTypeConst.ts] //// | ||
|
|
||
| === classPropertyInferenceFromBroaderTypeConst.ts === | ||
| // Repro from GH#62264 | ||
| // Class property should infer the wider declared type (AB), not the narrowed literal type ("A") | ||
|
|
||
| type AB = 'A' | 'B'; | ||
| >AB : AB | ||
| > : ^^ | ||
|
|
||
| const DEFAULT: AB = 'A'; | ||
| >DEFAULT : AB | ||
| > : ^^ | ||
| >'A' : "A" | ||
| > : ^^^ | ||
|
|
||
| class C { | ||
| >C : C | ||
| > : ^ | ||
|
|
||
| D = DEFAULT; | ||
| >D : AB | ||
| > : ^^ | ||
| >DEFAULT : AB | ||
| > : ^^ | ||
|
|
||
| method() { | ||
| >method : () => void | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| switch (this.D) { | ||
| >this.D : AB | ||
| > : ^^ | ||
| >this : this | ||
| > : ^^^^ | ||
| >D : AB | ||
| > : ^^ | ||
|
|
||
| case 'A': break; | ||
| >'A' : "A" | ||
| > : ^^^ | ||
|
|
||
| case 'B': break; // should not error | ||
| >'B' : "B" | ||
| > : ^^^ | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // D should be AB, not "A" | ||
| declare const c: C; | ||
| >c : C | ||
| > : ^ | ||
|
|
||
| declare function expectAB(x: AB): void; | ||
| >expectAB : (x: AB) => void | ||
| > : ^ ^^ ^^^^^ | ||
| >x : AB | ||
| > : ^^ | ||
|
|
||
| expectAB(c.D); // ok | ||
| >expectAB(c.D) : void | ||
| > : ^^^^ | ||
| >expectAB : (x: AB) => void | ||
| > : ^ ^^ ^^^^^ | ||
| >c.D : AB | ||
| > : ^^ | ||
| >c : C | ||
| > : ^ | ||
| >D : AB | ||
| > : ^^ | ||
|
|
||
| c.D = 'B'; // ok | ||
| >c.D = 'B' : "B" | ||
| > : ^^^ | ||
| >c.D : AB | ||
| > : ^^ | ||
| >c : C | ||
| > : ^ | ||
| >D : AB | ||
| > : ^^ | ||
| >'B' : "B" | ||
| > : ^^^ | ||
|
|
||
| // Static property should work the same way | ||
| class D { | ||
| >D : D | ||
| > : ^ | ||
|
|
||
| static SD = DEFAULT; | ||
| >SD : AB | ||
| > : ^^ | ||
| >DEFAULT : AB | ||
| > : ^^ | ||
| } | ||
| D.SD = 'B'; // ok | ||
| >D.SD = 'B' : "B" | ||
| > : ^^^ | ||
| >D.SD : AB | ||
| > : ^^ | ||
| >D : typeof D | ||
| > : ^^^^^^^^ | ||
| >SD : AB | ||
| > : ^^ | ||
| >'B' : "B" | ||
| > : ^^^ | ||
|
|
31 changes: 31 additions & 0 deletions
31
tests/cases/compiler/classPropertyInferenceFromBroaderTypeConst.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // @strict: true | ||
|
|
||
| // Repro from GH#62264 | ||
| // Class property should infer the wider declared type (AB), not the narrowed literal type ("A") | ||
|
|
||
| type AB = 'A' | 'B'; | ||
|
|
||
| const DEFAULT: AB = 'A'; | ||
|
|
||
| class C { | ||
| D = DEFAULT; | ||
|
|
||
| method() { | ||
| switch (this.D) { | ||
| case 'A': break; | ||
| case 'B': break; // should not error | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // D should be AB, not "A" | ||
| declare const c: C; | ||
| declare function expectAB(x: AB): void; | ||
| expectAB(c.D); // ok | ||
| c.D = 'B'; // ok | ||
|
|
||
| // Static property should work the same way | ||
| class D { | ||
| static SD = DEFAULT; | ||
| } | ||
| D.SD = 'B'; // ok |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be worth rechecking if this now shouldn't include
PropagatesThisKeywordtooThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Andarist how does this manifest? I tried to cook up a repro but failed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the time of writing the comment, I didn't think of any specific case. I just knew this change might change the presence of this flag on some property declarations so it would be worth double-checking if that can lead to some adverse effects.
But like you and your agents, my agents and I can't find any either. 🤪
So my vote goes for "internal change without public-facing downsides"