-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[WIP] Fix regression in 6.0: Broader type inference issue #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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3869,6 +3869,9 @@ export function getContainerFlags(node: Node): ContainerFlags { | |
| case SyntaxKind.ModuleBlock: | ||
| return ContainerFlags.IsControlFlowContainer; | ||
|
|
||
| case SyntaxKind.PropertyDeclaration: | ||
| return (node as PropertyDeclaration).initializer ? ContainerFlags.IsControlFlowContainer : ContainerFlags.None; | ||
|
|
||
|
Comment on lines
+3872
to
+3874
|
||
| case SyntaxKind.CatchClause: | ||
| case SyntaxKind.ForStatement: | ||
| case SyntaxKind.ForInStatement: | ||
|
|
||
| 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 |
| 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)) | ||
|
|
| 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" | ||
| > : ^^^ | ||
|
|
| 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 |
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
PropagatesThisKeywordtoo