6/24/2023
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 "Zurich", "Oslo" and "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.
"Zurich", "Oslo" or "London","Oslo" or "London","London" only,never typeThis 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
Start: https://lnkd.in/gt7ePcAx
End: https://lnkd.in/gzbwcvBR
If you want to see more of this. Please let me know!