MENU

Fun & Interesting

Let's Fix The Fatal Flaw in TypeScript's Union Types!

Typed Rocks 25,778 7 months ago
Video Not Working? Fix It Now

Code: https://github.com/typed-rocks/typescript/blob/main/one_of.ts never type: https://www.youtube.com/watch?v=TBxln_ERlSE recursive types: https://www.youtube.com/watch?v=Bqe_5-zvk1U& Union types in TypeScript are great, most of the time. But sometimes we want to be restricted to use only one of the types in the union. TypeScript itself doesn't provide such a type which can lead to many issues when not addressed properly. In this video we will fix this by creating our own "OneOf" type, which will ensure exactly that. 0:00 Identify The Flaw 0:52 A Manual Solution 2:30 SimpleOneOf implementation 5:30 OneOf implementation 13:20 Outro

Comment