You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeProcess=undefined|{exitCode: number|null}constisNull=(value: unknown): value is null{returnvalue===null;};letprocess: Process;if(isNull(process?.exitCode)){process.exitCode// ^?}
We are narrowing process.exitCode, but indirectly we should be also narrowing the parent objects, which in this case is just process. Basically if process.exitCode is null then process can't be undefined, 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:
fabiospampinato
changed the title
Propagating type guards to parent objects
Propagating information from type guards to parent objects
Aug 17, 2023
@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:
π 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: