-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Use comparability for discriminant properties when narrowing types for a default switch clause #61211
Open
Andarist
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
Andarist:fix/narrow-type-by-switch-on-discriminant
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.
+540
−1
Open
Use comparability for discriminant properties when narrowing types for a default switch clause #61211
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 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
180 changes: 180 additions & 0 deletions
180
tests/baselines/reference/discriminatedUnionTypes4.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,180 @@ | ||
//// [tests/cases/conformance/types/union/discriminatedUnionTypes4.ts] //// | ||
|
||
=== discriminatedUnionTypes4.ts === | ||
// https://github.com/microsoft/TypeScript/issues/61207 | ||
|
||
enum AnimalType { | ||
>AnimalType : Symbol(AnimalType, Decl(discriminatedUnionTypes4.ts, 0, 0)) | ||
|
||
cat = "cat", | ||
>cat : Symbol(AnimalType.cat, Decl(discriminatedUnionTypes4.ts, 2, 17)) | ||
|
||
dog = "dog", | ||
>dog : Symbol(AnimalType.dog, Decl(discriminatedUnionTypes4.ts, 3, 14)) | ||
} | ||
|
||
type Animal = | ||
>Animal : Symbol(Animal, Decl(discriminatedUnionTypes4.ts, 5, 1)) | ||
|
||
| { | ||
type: `${AnimalType.cat}`; | ||
>type : Symbol(type, Decl(discriminatedUnionTypes4.ts, 8, 5)) | ||
>AnimalType : Symbol(AnimalType, Decl(discriminatedUnionTypes4.ts, 0, 0)) | ||
>cat : Symbol(AnimalType.cat, Decl(discriminatedUnionTypes4.ts, 2, 17)) | ||
|
||
meow: string; | ||
>meow : Symbol(meow, Decl(discriminatedUnionTypes4.ts, 9, 32)) | ||
} | ||
| { | ||
type: `${AnimalType.dog}`; | ||
>type : Symbol(type, Decl(discriminatedUnionTypes4.ts, 12, 5)) | ||
>AnimalType : Symbol(AnimalType, Decl(discriminatedUnionTypes4.ts, 0, 0)) | ||
>dog : Symbol(AnimalType.dog, Decl(discriminatedUnionTypes4.ts, 3, 14)) | ||
|
||
bark: string; | ||
>bark : Symbol(bark, Decl(discriminatedUnionTypes4.ts, 13, 32)) | ||
|
||
}; | ||
|
||
function check(p: never) { | ||
>check : Symbol(check, Decl(discriminatedUnionTypes4.ts, 15, 6)) | ||
>p : Symbol(p, Decl(discriminatedUnionTypes4.ts, 17, 15)) | ||
|
||
throw new Error("Error!"); | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
} | ||
|
||
function action1(animal: Animal) { | ||
>action1 : Symbol(action1, Decl(discriminatedUnionTypes4.ts, 19, 1)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 21, 17)) | ||
>Animal : Symbol(Animal, Decl(discriminatedUnionTypes4.ts, 5, 1)) | ||
|
||
if (animal.type === AnimalType.cat) { | ||
>animal.type : Symbol(type, Decl(discriminatedUnionTypes4.ts, 8, 5), Decl(discriminatedUnionTypes4.ts, 12, 5)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 21, 17)) | ||
>type : Symbol(type, Decl(discriminatedUnionTypes4.ts, 8, 5), Decl(discriminatedUnionTypes4.ts, 12, 5)) | ||
>AnimalType.cat : Symbol(AnimalType.cat, Decl(discriminatedUnionTypes4.ts, 2, 17)) | ||
>AnimalType : Symbol(AnimalType, Decl(discriminatedUnionTypes4.ts, 0, 0)) | ||
>cat : Symbol(AnimalType.cat, Decl(discriminatedUnionTypes4.ts, 2, 17)) | ||
|
||
console.log(animal.meow); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>animal.meow : Symbol(meow, Decl(discriminatedUnionTypes4.ts, 9, 32)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 21, 17)) | ||
>meow : Symbol(meow, Decl(discriminatedUnionTypes4.ts, 9, 32)) | ||
|
||
} else if (animal.type === AnimalType.dog) { | ||
>animal.type : Symbol(type, Decl(discriminatedUnionTypes4.ts, 12, 5)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 21, 17)) | ||
>type : Symbol(type, Decl(discriminatedUnionTypes4.ts, 12, 5)) | ||
>AnimalType.dog : Symbol(AnimalType.dog, Decl(discriminatedUnionTypes4.ts, 3, 14)) | ||
>AnimalType : Symbol(AnimalType, Decl(discriminatedUnionTypes4.ts, 0, 0)) | ||
>dog : Symbol(AnimalType.dog, Decl(discriminatedUnionTypes4.ts, 3, 14)) | ||
|
||
console.log(animal.bark); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>animal.bark : Symbol(bark, Decl(discriminatedUnionTypes4.ts, 13, 32)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 21, 17)) | ||
>bark : Symbol(bark, Decl(discriminatedUnionTypes4.ts, 13, 32)) | ||
|
||
} else { | ||
check(animal); | ||
>check : Symbol(check, Decl(discriminatedUnionTypes4.ts, 15, 6)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 21, 17)) | ||
} | ||
} | ||
|
||
function action2(animal: Animal) { | ||
>action2 : Symbol(action2, Decl(discriminatedUnionTypes4.ts, 29, 1)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 31, 17)) | ||
>Animal : Symbol(Animal, Decl(discriminatedUnionTypes4.ts, 5, 1)) | ||
|
||
switch (animal.type) { | ||
>animal.type : Symbol(type, Decl(discriminatedUnionTypes4.ts, 8, 5), Decl(discriminatedUnionTypes4.ts, 12, 5)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 31, 17)) | ||
>type : Symbol(type, Decl(discriminatedUnionTypes4.ts, 8, 5), Decl(discriminatedUnionTypes4.ts, 12, 5)) | ||
|
||
case `${AnimalType.cat}`: | ||
>AnimalType.cat : Symbol(AnimalType.cat, Decl(discriminatedUnionTypes4.ts, 2, 17)) | ||
>AnimalType : Symbol(AnimalType, Decl(discriminatedUnionTypes4.ts, 0, 0)) | ||
>cat : Symbol(AnimalType.cat, Decl(discriminatedUnionTypes4.ts, 2, 17)) | ||
|
||
console.log(animal.meow); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>animal.meow : Symbol(meow, Decl(discriminatedUnionTypes4.ts, 9, 32)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 31, 17)) | ||
>meow : Symbol(meow, Decl(discriminatedUnionTypes4.ts, 9, 32)) | ||
|
||
break; | ||
case `${AnimalType.dog}`: | ||
>AnimalType.dog : Symbol(AnimalType.dog, Decl(discriminatedUnionTypes4.ts, 3, 14)) | ||
>AnimalType : Symbol(AnimalType, Decl(discriminatedUnionTypes4.ts, 0, 0)) | ||
>dog : Symbol(AnimalType.dog, Decl(discriminatedUnionTypes4.ts, 3, 14)) | ||
|
||
console.log(animal.bark); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>animal.bark : Symbol(bark, Decl(discriminatedUnionTypes4.ts, 13, 32)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 31, 17)) | ||
>bark : Symbol(bark, Decl(discriminatedUnionTypes4.ts, 13, 32)) | ||
|
||
break; | ||
default: | ||
check(animal); | ||
>check : Symbol(check, Decl(discriminatedUnionTypes4.ts, 15, 6)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 31, 17)) | ||
} | ||
} | ||
|
||
function action3(animal: Animal) { | ||
>action3 : Symbol(action3, Decl(discriminatedUnionTypes4.ts, 42, 1)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 44, 17)) | ||
>Animal : Symbol(Animal, Decl(discriminatedUnionTypes4.ts, 5, 1)) | ||
|
||
switch (animal.type) { | ||
>animal.type : Symbol(type, Decl(discriminatedUnionTypes4.ts, 8, 5), Decl(discriminatedUnionTypes4.ts, 12, 5)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 44, 17)) | ||
>type : Symbol(type, Decl(discriminatedUnionTypes4.ts, 8, 5), Decl(discriminatedUnionTypes4.ts, 12, 5)) | ||
|
||
case AnimalType.cat: | ||
>AnimalType.cat : Symbol(AnimalType.cat, Decl(discriminatedUnionTypes4.ts, 2, 17)) | ||
>AnimalType : Symbol(AnimalType, Decl(discriminatedUnionTypes4.ts, 0, 0)) | ||
>cat : Symbol(AnimalType.cat, Decl(discriminatedUnionTypes4.ts, 2, 17)) | ||
|
||
console.log(animal.meow); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>animal.meow : Symbol(meow, Decl(discriminatedUnionTypes4.ts, 9, 32)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 44, 17)) | ||
>meow : Symbol(meow, Decl(discriminatedUnionTypes4.ts, 9, 32)) | ||
|
||
break; | ||
case AnimalType.dog: | ||
>AnimalType.dog : Symbol(AnimalType.dog, Decl(discriminatedUnionTypes4.ts, 3, 14)) | ||
>AnimalType : Symbol(AnimalType, Decl(discriminatedUnionTypes4.ts, 0, 0)) | ||
>dog : Symbol(AnimalType.dog, Decl(discriminatedUnionTypes4.ts, 3, 14)) | ||
|
||
console.log(animal.bark); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>animal.bark : Symbol(bark, Decl(discriminatedUnionTypes4.ts, 13, 32)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 44, 17)) | ||
>bark : Symbol(bark, Decl(discriminatedUnionTypes4.ts, 13, 32)) | ||
|
||
break; | ||
default: | ||
check(animal); | ||
>check : Symbol(check, Decl(discriminatedUnionTypes4.ts, 15, 6)) | ||
>animal : Symbol(animal, Decl(discriminatedUnionTypes4.ts, 44, 17)) | ||
} | ||
} | ||
|
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.
This essentially matches the logic used by
narrowTypeByEquality
hereAs we can see, the
valueType
has to passisUnitType
there and the equivalent of thevalueType
here are all the types inswitchTypes
. Note that in the case of switch/case here that check mainly ensures we ignoreneverType
as that's comparable to everything.neverType
is specifically used withinswitchTypes
to indicate that this switch statement has a default case (seegetTypeOfSwitchClause
here andhasDefaultClause
here)