Fix switch-true case-true narrowing to behave equivalently to default case#59459
Fix switch-true case-true narrowing to behave equivalently to default case#59459Zzzen wants to merge 2 commits intomicrosoft:mainfrom
Conversation
|
@typescript-bot test it |
|
Hey @jakebailey, the results of running the DT tests are ready. Everything looks the same! |
|
@jakebailey Here are the results of running the user tests with tsc comparing Everything looks good! |
|
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@jakebailey Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
|
Would this also fix this function's return type being inferred as type Circle = {
kind: "circle";
radius: number;
};
type Square = {
kind: "square";
sideLength: number;
};
type Shape = Circle | Square;
function calculateArea(shape: Shape) {
switch (true) {
case shape.kind === "circle": {
return Math.PI * shape.radius * shape.radius;
}
case shape.kind === "square": {
return shape.sideLength * shape.sideLength;
}
}
} |
| // @noEmit: true | ||
|
|
||
| function uhoh(input: string) : "A"|"B"|"C" { | ||
| switch (true) { |
There was a problem hiding this comment.
Awful, but, what about switch (false) + case false:?
There was a problem hiding this comment.
To my surprise, it seems some folks are writing switch(false). No case false: found though. Guess it wouldn't hurt to support it too! https://github.com/search?q=switch%28false%29%7B+language%3Atypescript&type=code&p=1
There was a problem hiding this comment.
However, after some research, I don’t think we want to support it since switch(false) isn’t something we generally support.
type A = { type: "A" };
type B = { type: "B" };
type AorB = A | B;
const isA = (x: AorB): x is A => x.type === "A";
const isB = (x: AorB): x is B => x.type === "B";
function test1(x: AorB) {
switch (false) {
case isA(x):
// in a perfect world, x would have type B
x;
break;
case isB(x):
// in a perfect world, x would have type A
x;
break;
}
}
function test2(x: AorB) {
switch (true) {
case isA(x):
x;
break;
case isB(x):
x;
break;
}
}
@Firsh no, you probably want to create a new issue for this. |
|
With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies). Next steps for PRs:
|
… case
Fixes #59133