-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Propagating information from type guards to parent objects #55422
Comments
fabiospampinato
changed the title
Propagating type guards to parent objects
Propagating information from type guards to parent objects
Aug 17, 2023
Duplicate of #42384. |
Aaaaah, missed that, thanks! |
@fabiospampinato Note that this form of narrowing (narrowing a parent object to be non-null off the result of an optional chain) actually already works when done inline: let process: Process;
if (process?.exitCode === null) {
process.exitCode // ok
} Not sure why it doesn't work with UDTGs. I guess technically a type predicate can lie but the compiler generally assumes they don't. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π Search Terms
β Viability Checklist
β Suggestion
An example is worth a 1000 words:
We are narrowing
process.exitCode
, but indirectly we should be also narrowing the parent objects, which in this case is justprocess
. Basically if process.exitCode isnull
thenprocess
can't beundefined
, still, TS thinks it might still be.π Motivating Example
See above.
π» Use Cases
Generally the smarter the type checker the better, and the cleaner the code you can write gets.
The text was updated successfully, but these errors were encountered: