-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fix issue with optional chaining and type inference in type guard #55613
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
Conversation
@typescript-bot test top100 |
Heya @jakebailey, I've started to run the diff-based top-repos suite on this PR at 1fddcab. You can monitor the build here. Update: The results are in! |
Heya @jakebailey, I've started to run the regular perf test suite on this PR at 1fddcab. You can monitor the build here. Update: The results are in! |
Heya @jakebailey, I've started to run the diff-based user code test suite on this PR at 1fddcab. You can monitor the build here. Update: The results are in! |
Heya @jakebailey, I've started to run the parallelized Definitely Typed test suite on this PR at 1fddcab. You can monitor the build here. Update: The results are in! |
@jakebailey Here are the results of running the user test suite comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Everything looks good! |
@jakebailey Here they are:
CompilerComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
tsserverComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
StartupComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@jakebailey Here are the results of running the top-repos suite comparing Everything looks good! |
Hey @jakebailey, the results of running the DT tests are ready. |
}; | ||
|
||
const getName2 = (person?: Person): string => { | ||
return isString(person?.name) ? person?.name : ''; |
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.
Shouldn't this be changed to this?
return isString(person?.name) ? person?.name : ''; | |
return isString(person?.name) ? person.name : ''; |
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.
Both should be fine. If I recall correctly, optional chaining does not alter the type of the expression it is applied to.
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.
Ye, right - i thought that previously the error was also raised within the truthy branch of the conditional expression (as in that the obj wasnt being proven to be non-nullable) but it seems that this part was already working ok
type Person = { name: string; } | ||
|
||
const getName1 = (person?: Person): string => { | ||
return typeof person?.name === 'string' ? person?.name : ''; |
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.
similarly here:
return typeof person?.name === 'string' ? person?.name : ''; | |
return typeof person?.name === 'string' ? person.name : ''; |
It must be getting too close to spooky season because I read the issue title as "Fix issue with ... type gourd" 🎃 |
Fixes #34974