Typescript's Most Misunderstood Feature: Unions and Intersections

  Рет қаралды 1,531

If Coding Were Natural

If Coding Were Natural

Күн бұрын

Пікірлер: 12
@elielberra2867
@elielberra2867 11 ай бұрын
Amazing explanation! Thank you for sharing the results of your research and demonstrating with clear examples :)
@vencler
@vencler Жыл бұрын
Hey what terminal/shell/editor did you use in this video? And do you have color scheme configuration for it? Talking about 4:10 to 7:10 segment of the video. Thank you!
@orrshalev2604
@orrshalev2604 2 жыл бұрын
Amazing videos! Hope you continue to upload, I’m sure you will blow up very soon :-)
@echobucket
@echobucket 2 жыл бұрын
User Land vs Type Land is really Run Time vs Compile Time
@Cwstra
@Cwstra 2 жыл бұрын
I’ll go ahead and send any readers of this comment down another rabbit hole: this behavior is related to two concepts: covariance and contravariance. For these examples, assume we were using the `type Pet = Dog | Cat` definition. Say we had an (immutable) `List` type, with methods like `map` and `filter`. We can (correctly) intuit that, since a `Cat` is a `Pet`, a `List` is also a `List`. This behavior of `List`s is known as covariance: if `A` is a `B`, then a `List` is a `List` For another example, think about a (readonly) `Record`. We could definitely use this wherever a (readonly) `Record` was needed, so (readonly) `Record` is also covariant, at least as far as `T` is concerned. Now think about a very different type: actions, which we could define as `type Action = (val: T) => void`. Is `Action` covariant? Well, here’s an `Action`: `(cat: Cat) => cat.meow()`. If we tried to pass in a `Pet` instead, we might end up with an exception: that `Pet` could be a `Dog`, which cannot `meow`. So it really doesn’t make sense to say that a `Action` is a `Action`. What about a `Action`, say `(pet: Pet) => pet.eat()`? Well, it’d be perfectly fine to pass a `Cat` or `Dog` here; after all, both can `eat`. So because a `Cat` is a `Pet`, an `Action` is an `Action`, which is backwards from how `List`s work. We call this contravariance: if `A` is a `B`, an `Action` is an `Action`. Let’s look back at readonly records again: what if we were talking about the key type, rather than the value? Think about a `Record` and a `Record`. Obviously, anything that’s an `"eat"` is also an `"eat" | "meow"`. But a `Record` definitely has the one property an object needs to be a `Record`, namely, `"eat"`. So from that, if `A` is a `B`, then a `Record` is a `Record`; contravariance again, This last point is crucial to the phenomena in the video: because a union of objects could be _any_ of the objects, the only safe properties to access on a union are the ones which are shared between them. The contravariance of the keys sorta flips the union of objects into an intersection of their keys. All that to say, it’s not so much Typescript’s fault, but just the weirder points of how types need to work.
@nashmcguire5771
@nashmcguire5771 Жыл бұрын
Good stuff👌
@brianhayes1105
@brianhayes1105 2 жыл бұрын
Very nice.
@muzzamilraza
@muzzamilraza 2 жыл бұрын
Which terminal emulator you are using?
@techworld3043
@techworld3043 Жыл бұрын
well nice video, but i could not understand that much. i should start learning typescript.
@mvargasmoran
@mvargasmoran 2 жыл бұрын
I hate this, what's the point? to look more clever by using broken stuff? Typescript is just another JS
@ifcodingwerenatural
@ifcodingwerenatural Жыл бұрын
Honestly though, the more you get into Typescript, the more you realize it's kind of a messed up language sitting on top of another messed up language 😂
@mvargasmoran
@mvargasmoran Жыл бұрын
@@ifcodingwerenatural I found it baffling that it didn't solved some of the JS weirdness, why are you making a new lang just to preserve the worst parts of the one you are supposed to replace? 😔
An Accurate Explanation of Microservices
10:58
If Coding Were Natural
Рет қаралды 1,2 М.
Union Types in TS
14:49
Hitesh Choudhary
Рет қаралды 17 М.
Арыстанның айқасы, Тәуіржанның шайқасы!
25:51
QosLike / ҚосЛайк / Косылайық
Рет қаралды 700 М.
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН
Сестра обхитрила!
00:17
Victoria Portfolio
Рет қаралды 958 М.
Let's Fix The Fatal Flaw in TypeScript's Union Types!
13:45
Typed Rocks
Рет қаралды 24 М.
Are your TypeScript Unions broken? | Advanced TypeScript
7:36
Andrew Burgess
Рет қаралды 8 М.
any vs unknown vs never: TypeScript demystified
8:01
Andrew Burgess
Рет қаралды 26 М.
"Understanding TypeScript's Structural Type System" by Drew Colthorp
37:47
Strange Loop Conference
Рет қаралды 14 М.
I am not sorry for switching to C
11:34
Sheafification of G
Рет қаралды 238 М.
Someone improved my code by 40,832,277,770%
28:47
Stand-up Maths
Рет қаралды 2,7 МЛН
"C" Programming Language: Brian Kernighan - Computerphile
8:26
Computerphile
Рет қаралды 1,9 МЛН
HashMaps & Dictionaries, Explained Simply
22:44
Nic Barker
Рет қаралды 17 М.
Branded Types give you stronger input validation
9:22
Andrew Burgess
Рет қаралды 19 М.
The KEY to unions and intersections in TypeScript
4:32
Andrew Burgess
Рет қаралды 11 М.
Арыстанның айқасы, Тәуіржанның шайқасы!
25:51
QosLike / ҚосЛайк / Косылайық
Рет қаралды 700 М.