View a live demo here: https://lnkd.in/gMH7u—i
Assigning the switch expression to a never-typed variable will cause a compile-time error.
I like to think of types as boxes.
At the start, we have a union of the strings
"London". These are the potential types inside the “location” box.
As we move down the switch statement and handle each case, we pull out these options out of the box.
This is really useful if you plan on adding more options to your union types. If we forget to pull out a option out of the Locations box, we’ll get a compile-time error.
So we’ll know straightaway that we’ve make a mistake.
That’s an introduction to Type Narrowing in TypeScript.
Deep Dive into Advanced TypeScript: A Live Coding Expedition
NDC Oslo 2023
If you want to see more of this. Please let me know!