-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Fixed an issue with missingType
narrowing by case undefined
in default cases
#58001
Fixed an issue with missingType
narrowing by case undefined
in default cases
#58001
Conversation
@typescript-bot test it |
I'm curious why we don't just put the regular We should source out all the "compiler-inserted function func2(arg: A) {
// Under noUncheckedIndexedAccess
const optionalProp = ["hello" as const][Math.random()];
switch (optionalProp) {
case undefined:
case "hello":
return;
default:
// Should OK
assertUnreachable(optionalProp)
}
} plus vary this test by all quadrants of NUIA / EOPT |
@typescript-bot pack this |
Hey @RyanCavanaugh, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
Hey @RyanCavanaugh, the results of running the DT tests are ready. Everything looks the same! |
@RyanCavanaugh Here are the results of running the user tests comparing Everything looks good! |
@RyanCavanaugh Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
Yeah, same. I actually have thought that this missing type should be converted to undefined way earlier but I could not find evidence of that anywhere right now.
I have 2 other open PRs related to EOPT ;p so yes, this is very easy to miss and I wouldn't be surprised if more bugs like this are still out there. |
@@ -28753,7 +28753,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||
if (!hasDefaultClause) { | |||
return caseType; | |||
} | |||
const defaultType = filterType(type, t => !(isUnitLikeType(t) && contains(switchTypes, getRegularTypeOfLiteralType(extractUnitType(t))))); | |||
const defaultType = filterType(type, t => !(isUnitLikeType(t) && contains(switchTypes, t.flags & TypeFlags.Undefined ? undefinedType : getRegularTypeOfLiteralType(extractUnitType(t))))); |
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.
a potential alternative to this could look like this:
const defaultType = filterType(type, t => !(isUnitLikeType(t) && contains(switchTypes, t.flags & TypeFlags.Undefined ? undefinedType : getRegularTypeOfLiteralType(extractUnitType(t))))); | |
const switchTypesUnion = getUnionType(filter(switchTypes, isUnitType)); | |
const defaultType = filterType(type, t => !(isUnitLikeType(t) && areTypesComparable(t, switchTypesUnion))); |
this is inspired by the logic used by narrowTypeByEquality
and I've checked that this alternative would still pass the existing test cases.
@RyanCavanaugh Here are the results of running the top 400 repos comparing Everything looks good! |
Behavior / implementation seem fine but I'd love to know if there's a more comprehensive fix |
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.
Looks completed very well!
fixes #57999