|
|
|
|
|
by masklinn
1544 days ago
|
|
> It surely is nice to get the same guarantees without _needing_ a switch statement. Is it? let data: Some(usize) = f();
match data {
Some(pages) => pages + 5,
None => 0
}
Seems pretty nice.Or if you want a more structurally similar code with a guard (and removing the useless bits): let Some(pages) = f() else {
return 0
};
pages + 5
|
|
Never said it's not. I'm just saying it's nice to have type narrowing everywhere since it includes block based switching/matching as well (what you're arguging is nice, which I'd agree)
You'll have to use your imagination here because in typescript narrowing works on all conditionals (ternaries, boolean switches, etc), so naturally there are many more examples where it's useful. It's easy to trivialize any specific example.
E.g. they're useful in JSX when you want to do a one-line type-safe boolean switches like: {node && <RenderNode node={node}/>}.