-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12538 from Microsoft/fixExhaustiveSwitchCheck
Fix exhaustive switch check
- Loading branch information
Showing
5 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
39 changes: 39 additions & 0 deletions
39
tests/baselines/reference/exhaustiveSwitchWithWideningLiteralTypes.js
This file contains 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,39 @@ | ||
//// [exhaustiveSwitchWithWideningLiteralTypes.ts] | ||
|
||
// Repro from #12529 | ||
|
||
class A { | ||
readonly kind = "A"; // (property) A.kind: "A" | ||
} | ||
|
||
class B { | ||
readonly kind = "B"; // (property) B.kind: "B" | ||
} | ||
|
||
function f(value: A | B): number { | ||
switch(value.kind) { | ||
case "A": return 0; | ||
case "B": return 1; | ||
} | ||
} | ||
|
||
//// [exhaustiveSwitchWithWideningLiteralTypes.js] | ||
// Repro from #12529 | ||
var A = (function () { | ||
function A() { | ||
this.kind = "A"; // (property) A.kind: "A" | ||
} | ||
return A; | ||
}()); | ||
var B = (function () { | ||
function B() { | ||
this.kind = "B"; // (property) B.kind: "B" | ||
} | ||
return B; | ||
}()); | ||
function f(value) { | ||
switch (value.kind) { | ||
case "A": return 0; | ||
case "B": return 1; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/baselines/reference/exhaustiveSwitchWithWideningLiteralTypes.symbols
This file contains 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,33 @@ | ||
=== tests/cases/compiler/exhaustiveSwitchWithWideningLiteralTypes.ts === | ||
|
||
// Repro from #12529 | ||
|
||
class A { | ||
>A : Symbol(A, Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 0, 0)) | ||
|
||
readonly kind = "A"; // (property) A.kind: "A" | ||
>kind : Symbol(A.kind, Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 3, 9)) | ||
} | ||
|
||
class B { | ||
>B : Symbol(B, Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 5, 1)) | ||
|
||
readonly kind = "B"; // (property) B.kind: "B" | ||
>kind : Symbol(B.kind, Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 7, 9)) | ||
} | ||
|
||
function f(value: A | B): number { | ||
>f : Symbol(f, Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 9, 1)) | ||
>value : Symbol(value, Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 11, 11)) | ||
>A : Symbol(A, Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 0, 0)) | ||
>B : Symbol(B, Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 5, 1)) | ||
|
||
switch(value.kind) { | ||
>value.kind : Symbol(kind, Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 3, 9), Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 7, 9)) | ||
>value : Symbol(value, Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 11, 11)) | ||
>kind : Symbol(kind, Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 3, 9), Decl(exhaustiveSwitchWithWideningLiteralTypes.ts, 7, 9)) | ||
|
||
case "A": return 0; | ||
case "B": return 1; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
tests/baselines/reference/exhaustiveSwitchWithWideningLiteralTypes.types
This file contains 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,40 @@ | ||
=== tests/cases/compiler/exhaustiveSwitchWithWideningLiteralTypes.ts === | ||
|
||
// Repro from #12529 | ||
|
||
class A { | ||
>A : A | ||
|
||
readonly kind = "A"; // (property) A.kind: "A" | ||
>kind : "A" | ||
>"A" : "A" | ||
} | ||
|
||
class B { | ||
>B : B | ||
|
||
readonly kind = "B"; // (property) B.kind: "B" | ||
>kind : "B" | ||
>"B" : "B" | ||
} | ||
|
||
function f(value: A | B): number { | ||
>f : (value: A | B) => number | ||
>value : A | B | ||
>A : A | ||
>B : B | ||
|
||
switch(value.kind) { | ||
>value.kind : "A" | "B" | ||
>value : A | B | ||
>kind : "A" | "B" | ||
|
||
case "A": return 0; | ||
>"A" : "A" | ||
>0 : 0 | ||
|
||
case "B": return 1; | ||
>"B" : "B" | ||
>1 : 1 | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/cases/compiler/exhaustiveSwitchWithWideningLiteralTypes.ts
This file contains 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,18 @@ | ||
// @strictNullChecks: true | ||
|
||
// Repro from #12529 | ||
|
||
class A { | ||
readonly kind = "A"; // (property) A.kind: "A" | ||
} | ||
|
||
class B { | ||
readonly kind = "B"; // (property) B.kind: "B" | ||
} | ||
|
||
function f(value: A | B): number { | ||
switch(value.kind) { | ||
case "A": return 0; | ||
case "B": return 1; | ||
} | ||
} |