This Is One Of My Favorite TypeScript Features

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ส.ค. 2024
  • TypeScript Simplified Course: courses.webdevsimplified.com/...
    Discriminating unions sound really fancy and scary, but in reality they are a fairly simple concept that makes writing certain types of React code so much easier. In this video I explain exactly what discriminating unions are, how to use them, why they are so great, and the limitations of them.
    📚 Materials/References:
    TypeScript Simplified Course: courses.webdevsimplified.com/...
    🌎 Find Me Here:
    My Blog: blog.webdevsimplified.com
    My Courses: courses.webdevsimplified.com
    Patreon: / webdevsimplified
    Twitter: / devsimplified
    Discord: / discord
    GitHub: github.com/WebDevSimplified
    CodePen: codepen.io/WebDevSimplified
    ⏱️ Timestamps:
    00:00 - Introduction
    00:22 - The Problem
    01:24 - Discriminating Union Basics
    03:58 - Discriminating Union Advanced
    #DiscriminatingUnion #WDS #TypeScript

ความคิดเห็น • 175

  • @WebDevSimplified
    @WebDevSimplified  8 หลายเดือนก่อน +6

    Checkout my full TypeScript Simplified course:
    courses.webdevsimplified.com/typescript-simplified/?ZCLQ8

    • @feha92
      @feha92 8 หลายเดือนก่อน

      You could also achieve this with algebraic data types (ADTs) such as "sum" types, instead of discriminating unions.
      ps: yes, I am aware that they are names for the same thing (and even more exists, "tagged unions" or "union types" is also common (though "union types" are arguably different, as some argue that the lack of a [usually internal] "state" field means "int | int | float" is the same as "int | float". This all comes down to idiolects regarding nomenclature though)), that is the joke :p Just much prefer "sum types" as a term ;D
      pps: Must admit I never saw someone call them "discriminating unions" before (besides wikipedia's list of synonyms) though.
      ppps: not rly dont any typescript, but was surprised to see both that the discriminating tag had to be explicitly written, and that the switch-case was done on the state field, rather than pattern-match the actual type of the LocationType itself (or match on the typeof), which would come with an implicit type-cast.

  • @jamienordmeyer4345
    @jamienordmeyer4345 8 หลายเดือนก่อน +249

    This is one of those patterns that feels so obvious when you see it, but would be easy to miss if you hadn't seen a direct example of it. Great topic for a video!!

    • @zlackbiro
      @zlackbiro 8 หลายเดือนก่อน +2

      One character of code can fix it, location.coord!.lat if you know the success case will always have coords. Just flag it with exclamation. LoL...😂

    • @dirknash4113
      @dirknash4113 7 หลายเดือนก่อน

      ​@@zlackbiroestás de broma, no? Esto que has dicho es una barbaridad 😂

    • @aArcziMetin2
      @aArcziMetin2 7 หลายเดือนก่อน +1

      It's definitely good feature, especially if you have array with mixed objects.

  • @WilhelmBerggren
    @WilhelmBerggren 8 หลายเดือนก่อน +29

    Nice video. I was hoping that you would have mentioned using "never" as a way to prevent Typescript from merging a union together. E.g. "type AorB = { a: true, b: never } | { a: never, b: true }"

  • @dashyftw
    @dashyftw 8 หลายเดือนก่อน +13

    Just a note. Discriminated unions should not be overused, as they take exponentially more time to compile the more "states" or branches of typing logic is added. TypeScript has a limit, so if your discriminated union is too complex, TypeScript will be slow and eventually will give you an error. For examples like in the video; its mostly fine; but when you start making discriminated unions over (for example) every HTMLElement subtype; you'll be unintentionally creating a sluggish TS environment.

  • @paullauren316
    @paullauren316 8 หลายเดือนก่อน +2

    Great video.
    I’ve come to expect great content from you and this one definitely doesn’t disappoint.
    Well done

    • @thewebdesigner
      @thewebdesigner 8 หลายเดือนก่อน

      th-cam.com/video/y5HCFAlx_z8/w-d-xo.html

  • @kaleb4093
    @kaleb4093 8 หลายเดือนก่อน

    Thank you for making this video while I'm preparing for a React TypeScript interview. Awesome timing

  • @codewithguillaume
    @codewithguillaume 8 หลายเดือนก่อน

    Thanks for this, Kyle !

  • @Patinhow100
    @Patinhow100 8 หลายเดือนก่อน

    As always, thank you for your greats videos!

  • @adimardev1550
    @adimardev1550 8 หลายเดือนก่อน

    this save tons of pain. thanks for saving us Kyle, very very useful content.

  • @nowayicommented1314
    @nowayicommented1314 8 หลายเดือนก่อน +29

    As a senior I’m using this at least once in every project. Great video 👍🏼

    • @zlackbiro
      @zlackbiro 8 หลายเดือนก่อน

      If you using this, you bloating your code. One character in code can fix it, location.coord!.lat if you know the success case will always have coords. Just flag it with exclamation. LoL...😂

    • @nowayicommented1314
      @nowayicommented1314 8 หลายเดือนก่อน +12

      @@zlackbiroyou don’t have real type safety that way tho. Maybe this works for your small projects but I also code for good DX so having stable & advanced typing is important. Also you’re not really bloating your code since the types will be removed before the code is shipped anyway 🤷🏼‍♂️

  • @StephenMoreira
    @StephenMoreira 8 หลายเดือนก่อน

    Very nice, learned something here, thanks for the video.

  • @jam_sessions6814
    @jam_sessions6814 8 หลายเดือนก่อน

    Beautiful line separated union type formatering!

  • @SeRoShadow
    @SeRoShadow 8 หลายเดือนก่อน +64

    so basically, if a type has varriants,
    create subtypes for each varriant
    then use those subtypes together as the type.

    • @pepperdayjackpac4521
      @pepperdayjackpac4521 8 หลายเดือนก่อน +2

      basically

    • @zlackbiro
      @zlackbiro 8 หลายเดือนก่อน

      One character of code can fix it, location.coord!.lat if you know the success case will always have coords. Just flag it with exclamation. LoL...😂

    • @SeRoShadow
      @SeRoShadow 8 หลายเดือนก่อน +1

      @@zlackbiro its a question mark ( ? ) and it is not Typescript

    • @ekcodes
      @ekcodes 8 หลายเดือนก่อน +16

      ​@@SeRoShadowhe's actually right. It's an exclamation mark (!). It's called the non null assertion operator and it's Typescript. Look it up before correcting people.

    • @dirknash4113
      @dirknash4113 7 หลายเดือนก่อน +2

      ​@@ekcodesbut this is an horrible solution.

  • @jayjani740
    @jayjani740 8 หลายเดือนก่อน

    Very useful one! Thanks

  • @Exilum
    @Exilum 8 หลายเดือนก่อน +1

    Thankfully I did use this feature. But more by instinct that actual knowledge. I just figured out mid-writing it could work and tested.
    I used it in the oppisite direction, to set a value based on the parameters passed. So then typescript knows to only allow related parameters if for example I set error to a value.

  • @nagendradevara1
    @nagendradevara1 8 หลายเดือนก่อน +1

    Angular provides strong support for TypeScript. Are you planning to create a video tutorial on Angular? You seem like the perfect candidate to simplify Angular for others.

  • @alontalmor
    @alontalmor 8 หลายเดือนก่อน

    Great video, thanks😊

  • @moretimeproductions
    @moretimeproductions 8 หลายเดือนก่อน

    This is powerful!

  • @ArchitecTJ37
    @ArchitecTJ37 8 หลายเดือนก่อน

    Well it's a good one........been learning TS and getting use to the writing style. Glad I earn something from this video.

  • @NewQuietBear
    @NewQuietBear 8 หลายเดือนก่อน

    I used this in my school project. It's neat.

  • @cggs3001
    @cggs3001 8 หลายเดือนก่อน

    Thanks man good stuff!

  • @ekomardiatno5517
    @ekomardiatno5517 8 หลายเดือนก่อน

    finally i found what i need, thanks!

  • @amirulidzham
    @amirulidzham 8 หลายเดือนก่อน

    Great! Thanks Kyle

  • @mauriciomueller86
    @mauriciomueller86 8 หลายเดือนก่อน

    Your content is awesome man, thanks for every single video that you created so far! Ps: You should teach us how to have a cool hair like yours 😃

  • @JohanNordberg
    @JohanNordberg 8 หลายเดือนก่อน

    Yes this is super useful! Something I wish C# had this natively as well.

  • @mralextacy
    @mralextacy 8 หลายเดือนก่อน

    excellent tip

  • @basdfgwe
    @basdfgwe 8 หลายเดือนก่อน

    This was actually really interesting.

  • @brobinbraauw5523
    @brobinbraauw5523 8 หลายเดือนก่อน

    Nice video, thanks

  • @harag9
    @harag9 8 หลายเดือนก่อน +6

    Nice, would have been better if you extended this and added in maybe a field that is available in all 3 states, as well as a field that is available is 2 of the states. or maybe optional fields in 2 but required in the 3rd. So many possibilities.

  • @jx4172
    @jx4172 8 หลายเดือนก่อน

    beautiful!

  • @codemadesimple1043
    @codemadesimple1043 8 หลายเดือนก่อน

    Well done 🎉

  • @AutisticThinker
    @AutisticThinker 8 หลายเดือนก่อน +24

    Nice. It's difficult to find advanced Typescript tutorials. Great work Kyle. 😀

    • @dog4ik
      @dog4ik 8 หลายเดือนก่อน +16

      Advanced? Matt Pocock is great option.

    • @Pilosofia
      @Pilosofia 8 หลายเดือนก่อน +4

      Try no BS TS from Jack Herrington.

  • @mritunjaykalyan2878
    @mritunjaykalyan2878 8 หลายเดือนก่อน

    Omg this is amazing. Previously I was using conditional check unnecessarily something like this
    error?error:’’
    Thank bro 😎

  • @ruggeddog3103
    @ruggeddog3103 3 หลายเดือนก่อน

    thank you kyle

  • @MithrilldGamer
    @MithrilldGamer 8 หลายเดือนก่อน +1

    Nice video!
    btw, I would like to see a video from you about Type Guards :)

  • @smoguesam7807
    @smoguesam7807 8 หลายเดือนก่อน

    Thank you

  • @48595157484812
    @48595157484812 8 หลายเดือนก่อน

    great info

  • @bowiemtl
    @bowiemtl 8 หลายเดือนก่อน

    Interesting, seems I have some more stuff to learn regarding typescript.

  • @yanike
    @yanike 8 หลายเดือนก่อน

    Awesome 😎

  • @imadragnZ
    @imadragnZ 8 หลายเดือนก่อน

    Gj one way to improve these kinds of demos is for me to tell wich design pattern or solid principal that could fit into ✌️

  • @ptomasz2453
    @ptomasz2453 8 หลายเดือนก่อน +1

    You can use type guard for last scenario

  • @hakuna_matata_hakuna
    @hakuna_matata_hakuna 8 หลายเดือนก่อน

    like your video styles, can we get a suspense video too? how to use it , best practices

  • @RishiRajxtrim
    @RishiRajxtrim 8 หลายเดือนก่อน

    Thanks

  • @Wielorybkek
    @Wielorybkek 8 หลายเดือนก่อน

    That was neat

  • @evolopterus
    @evolopterus 8 หลายเดือนก่อน

    What if you want to keep the previously loaded coords, while the state is loading the new ones, or even if there's an error?

  • @NikiHerl
    @NikiHerl 8 หลายเดือนก่อน +42

    You don't have to give each sub-type a name:
    export type FooBar = {
    type: "Foo"
    fooProp1: number
    } | {
    type: "Bar",
    barProp43: string
    }
    I prefer to write types like this - fewer names also mean fewer bad names, and less time spent devising them :)

    • @harag9
      @harag9 8 หลายเดือนก่อน +8

      Except proper naming of the type explains them better than just "bar"

    • @M0ns1gn0r
      @M0ns1gn0r 8 หลายเดือนก่อน +3

      ​@@harag9the proper name is already encoded in the type property.

    • @nomadshiba
      @nomadshiba 8 หลายเดือนก่อน +2

      also with method above, to select types individually you can do this:
      type Foo = Extract
      or this:
      type Foo = FooBar & { type: "Foo" }

  • @FunctionGermany
    @FunctionGermany 8 หลายเดือนก่อน +3

    i like video titles that make a viewer curious. if this video was titled something like "typescript discriminated unions" i might have not watched it because i already know about it.
    but what if someone is actively searching for this? then the video title would make it much harder to idenfity this video as a matching one, even with the tags, because they don't show up in search results.
    would be very interested in what you think about this, kyle!

    • @WebDevSimplified
      @WebDevSimplified  8 หลายเดือนก่อน +1

      That is a good point. It is really hard to balance this with search and the algorithm. I like to think that if someone searched for Discriminated Unions and this video popped up they would still click it since it is assumed this video is about what they are searching for but I am not 100% sure if they would.

    • @FunctionGermany
      @FunctionGermany 8 หลายเดือนก่อน +1

      @@WebDevSimplified do you think a system with a "subheadline" of sorts could help or could that discourage viewers that might not be interested?
      LinusTechTips uses more informational and less curious titles for their videos on their floatplane platform if i remember correctly.
      i wonder if a system that gave the option for both an eye-catching and a "dry" title would work on youtube.

    • @WebDevSimplified
      @WebDevSimplified  8 หลายเดือนก่อน +1

      That's an interesting thought. I doubt TH-cam would ever implement that, but it would be nice.

  • @parlor3115
    @parlor3115 8 หลายเดือนก่อน +1

    Hey Kyle. I've been a fan since the early days of wds. Problem is that I've been working on my dream project since pretty much day one of this channel, but I'm still not done with it (it's been 3 years already). My dream project is a Unix kernel developed using Rust (frontend and backend). I faced major hurdles while working on it, and now I feel it's never ending work. What am I doing wrong?
    PS: I talked with Linus Torvalds about collaborating, but he just flipped me the bird and stormed off yelling "Nvidia!!!".

  • @MoDlegion
    @MoDlegion 8 หลายเดือนก่อน +2

    You could just solve the last issue using a separate function with a typeguard though right? e.g.
    function isLoadingState(input: any): input is LoadingState
    {
    return typeof input.state === "string";
    }
    It is a little bit more verbose but it would work (add similar functions for the other options).

    • @aaronresing4692
      @aaronresing4692 8 หลายเดือนก่อน +2

      Dang, beat me to it. 😂

  • @U.S.ARANDOM
    @U.S.ARANDOM 8 หลายเดือนก่อน

    Nice

  • @paulosergioamorim_
    @paulosergioamorim_ 8 หลายเดือนก่อน

    The Rust Enums are good for that

  • @wij8044
    @wij8044 8 หลายเดือนก่อน

    You could keep the code from the start of the video and change your switch statement to read the data directly rather than rely on String literal to determine cases. For the “errors”, they’re not errors, you can use optical chaining.
    At the end of the day both solutions yours and mine) would still need testing and I prefer the simple solution. That said, it’s also wise to consider SOLID principles as you may have.

    • @lostinchina8585
      @lostinchina8585 8 หลายเดือนก่อน

      Typescript strict mode would not like that

  • @davideoreglio5232
    @davideoreglio5232 2 หลายเดือนก่อน

    does this work with enums instead of literal values?

  • @user-tt3bc7yk8r
    @user-tt3bc7yk8r 8 หลายเดือนก่อน

    Also found useful if you need to extend some type use intersection and union in one place.
    type Config = {//some properties//} & ({
    driver: "turso";
    dbCredentials: {
    url: string;
    authToken?: string;
    };
    } | {
    driver: "better-sqlite";
    dbCredentials: {
    url: string;
    };
    })

  • @Trekiros
    @Trekiros 8 หลายเดือนก่อน +1

    I always forget that this is considered the "special" type of union, since "regular" unions are so much more unintuitive to actually use to me

  • @saffanahmed9103
    @saffanahmed9103 8 หลายเดือนก่อน

    Can't we use a null check to simply resolve the error

  • @technologymad9253
    @technologymad9253 8 หลายเดือนก่อน

    Bro some hair care and style tips?

  • @linhub
    @linhub 8 หลายเดือนก่อน

    I see the "vvvvvvvvvvvvvvvv" do you use the WASD Code keyboard?

  • @SithLordBishop
    @SithLordBishop 8 หลายเดือนก่อน +2

    awesome, very concise and clean. Too bad there isn't a way to write this out a bit more shorthand or less verbose.

    • @zlackbiro
      @zlackbiro 8 หลายเดือนก่อน

      One character of code can fix it, location.coord!.lat if you know the success case will always have coords. Just flag it with exclamation. LoL...😂

  • @Hizbullla
    @Hizbullla 8 หลายเดือนก่อน +2

    I have massive issues with having to constantly check for undefined when working with optional properties using logical ANDs or ternaries. It results in bloated code that's unpleasant to look at. Is something like this an acceptable solution to it or are there more refined ways to do it?

    • @geralt36
      @geralt36 8 หลายเดือนก่อน +1

      The way shown in the video is the best way to do this as far as my knowledge of TS goes. I use it myself.

    • @Jufron97
      @Jufron97 8 หลายเดือนก่อน +4

      Another useful practice to distinguish between types is to use ‘in’, for example at the end of the video where you have Loading and Success state but only Success state has the coords property, you could say:
      if(“coords” in location){… your code} and inside those brackets ts will know you are in SuccesState.

    • @stevezelaznik5872
      @stevezelaznik5872 8 หลายเดือนก่อน

      It’s hard to know without looking at your code. Well written typescript makes a lot of other defensive programming unnecessary.

  • @gg.cip0t
    @gg.cip0t 8 หลายเดือนก่อน

    Should I really learn TS if i know JS. I tried but couldnt find any video where i can learn basics of ts in react and node.

    • @outis99
      @outis99 8 หลายเดือนก่อน +4

      Yes you absolutely should. Trust me you won't go back

    • @geralt36
      @geralt36 8 หลายเดือนก่อน

      Once you know TS well enough you can follow JS tutorials and convert them to TS yourself. It's not that hard. It's what I do as well. And yes, you should learn TS. It's the industry standard now. Pretty much every web dev job has TS as a requirement.

  • @ivanho570
    @ivanho570 8 หลายเดือนก่อน

    I cant believe I just faced the exact situation after watching this😂

  • @brNoMundo123
    @brNoMundo123 8 หลายเดือนก่อน

    Incrível 🇧🇷

  • @ayitinya
    @ayitinya 7 หลายเดือนก่อน

    How about discriminated Union Arrays, like type LocationState = (LoadingLocationState | SuccessLocationState)[]

  • @andykras
    @andykras 8 หลายเดือนก่อน

    how do you implement default case in TS to address 'unkown state' from backed?
    e.g., looks like "type safe" shit to me:
    ```ts
    default:
    console.log('unknown state:', (location as { state: string }).state)
    break
    ```
    you can't just write "location.state", because TS throws an error: Property 'state' does not exist on type 'never'. this is because TS doesn't know what to choose for "default" case. this is what I call the fake type safety, because naturally TS is JS, so writing types in TS you deluding yourself into a false sense of security aka "type safety". as ultimately TS compiles down to JS, and the types disappear in the runtime...

  • @web3made
    @web3made 8 หลายเดือนก่อน

    Awesome TS tutorial, but what about the issue of having too many types in your repo?

    • @NikiHerl
      @NikiHerl 8 หลายเดือนก่อน

      As I wrote in a comment above, you don't have to give each sub-type a name, just union | the type literals { type: "foo", ... } together.
      Though I have to ask: In what way is having

    • @SoreBrain
      @SoreBrain 8 หลายเดือนก่อน +3

      Stick to fewer "base" types and then use utility types where possible to derive from them and keep inferring types.
      Unless you have a good reason for it you should not have more types than minimally required.

    • @everyhandletaken
      @everyhandletaken 8 หลายเดือนก่อน

      You can stick them in another ts file & import them, all good

  • @Blafasel3
    @Blafasel3 8 หลายเดือนก่อน

    Couldnt you use a type guard on the last type to solve the issue? Wouldnt be of much help tho.
    And I always wondered why people would use different optional attributes which are either absent or have to be present depending on another field. Just implement a new interface or class and make it explicit like in this video... :)
    Sadly, I have seen this way too often 😅

  • @RayAndrewsDev
    @RayAndrewsDev 8 หลายเดือนก่อน

    Me : Takes a break from coding around a type problem and watch youtube to clear my head.
    Kyle: Hey that problem you're having .. here's the solution you hadn't though of
    Thanx :)

  • @anthonyyershov4156
    @anthonyyershov4156 8 หลายเดือนก่อน

    Aren't conditional types kind of like discriminating types but with generics?

  • @unknownmonkey8661
    @unknownmonkey8661 8 หลายเดือนก่อน

    Moar typescript hints!!1

  • @untlsn
    @untlsn 8 หลายเดือนก่อน +2

    I prefer to put error in success and data in error as maybe. Otherwise typescript will shout that, for example, error does not occur in the type Success
    Example:
    type SuccessLocation {
    state: 'Success'
    cords: { lat: number; lon: number }
    error?: { message: string }
    }

    • @mikel3377
      @mikel3377 8 หลายเดือนก่อน +1

      That defeats the entire point. The whole point of this video is to have separate type that you can discriminate between at runtime so you know what's defined or not where.

  • @chrisdaman4179
    @chrisdaman4179 8 หลายเดือนก่อน

    Is this alao called a tagged union?

    • @NikiHerl
      @NikiHerl 8 หลายเดือนก่อน +1

      Yup

  • @vojtechprazak533
    @vojtechprazak533 8 หลายเดือนก่อน

    There is no downside, you just write that if wrong, to make it work, you need to create typeguard, then it will work great. Personaly i would write it bit different, if you create base interafce with property state with type string, but even better is string enum or string union type (there is no room for error), then creating 2 other soecific interfaces 1st with error, 2nd with coords, then by checking state via typeguard you get final type without using type union.

    • @jomarcui2900
      @jomarcui2900 8 หลายเดือนก่อน

      Could you explain this a bit more? I am interested in your solution. Thanks in advanced! :)

  • @martijn3151
    @martijn3151 8 หลายเดือนก่อน +1

    If I may add. In your example, state would be a perfect candidate for an enum, as opposed to a hardcoded string.

  • @KenBanksPEng
    @KenBanksPEng 8 หลายเดือนก่อน +1

    chatGpt says:
    Why did the discriminated union break up with its partner?
    Because every time it tried to commit, it found itself in a different state!

  • @SebbeSebbe
    @SebbeSebbe 8 หลายเดือนก่อน +1

    Still looks weird to have arbitrary strings in the switch statements - will become hell to reactor "Success" into something else in the future. Can't state be an enum that holds the different state values to improve maintainability?

    • @SoreBrain
      @SoreBrain 8 หลายเดือนก่อน

      I also like enums but there is also noFallthroughCasesInSwitch that should help

    • @bass-tones
      @bass-tones 8 หลายเดือนก่อน +1

      They’re not arbitrary strings. TS is smart enough to know based on the object in the switch that all of the strings must be one of however many literals. If one of the cases was against a random string that didn’t match, the compiler would throw an error.
      If any of the types had their string label refactored, all spots depending on that apparent arbitrary string would break at compile time.

  • @amansarma417
    @amansarma417 8 หลายเดือนก่อน

    What to do when a package doesn't have a types support

  • @thenotoriousrt
    @thenotoriousrt 8 หลายเดือนก่อน +5

    This is good but i just think that theres to much naming going around. I just wanted to know whats your take on using mapped types for e.x:
    type LocationStateMapped = {
    Success: { state: "Success"; coords: { lat: number, lon: number } };
    Loading: { state: "Loading" };
    Error: { state: "Error"; error: { message: string } };
    };
    type LocationState = LocationStateMapped[keyof LocationStateMapped];

    • @mikel3377
      @mikel3377 8 หลายเดือนก่อน +1

      All just opinions, but I think the "type LocationState = LocationStateMapped[keyof LocationStateMapped];" makes this less readable. If you really want to go for terseness, I'd just define the single type straightforwardly: "type LocationState = {...} | {...} | {...}"

  • @denissorn
    @denissorn 8 หลายเดือนก่อน +1

    The state variable seems quite unnecessary here. Or... it would be superfluous if TS was a real language. Data type already communicates the info (kinda, b/c TS). The downside would be that one couldn't use the switch statement but would have to check for presence of say coordinates property.

    • @dilanboskan2222
      @dilanboskan2222 8 หลายเดือนก่อน +3

      Someone does not know how typescript works..

    • @NikiHerl
      @NikiHerl 8 หลายเดือนก่อน

      Typescript uses a structural typesystem, where indeed you need the discriminator variable, as opposed to a named typesystem (Java, ...), where instead the named type (class) information is around at runtime.
      This has nothing to do with "real" language or not - both systems have their advantages (syntactically) and can achieve pretty much the same things.

    • @denissorn
      @denissorn 8 หลายเดือนก่อน

      I don't, unfortunately neither do you it seems. Or maybe you have a better way of figuring out the type during runtime?

    • @denissorn
      @denissorn 8 หลายเดือนก่อน +1

      @@NikiHerl LOl. Of course it has to do with it being a "real" language or not, because it compiles to JS, so types do not exist at runtime. But yeah it is what it is.

    • @adambickford8720
      @adambickford8720 8 หลายเดือนก่อน

      @@denissorn It's a different way of doing types with its own tradeoffs, it's not some shortcoming that couldn't be avoided. It's a deliberate design choice and its wonderfully productive.
      In something like Java you have what are known as 'nominal' types. Java doesn't have to check the 'shape' of the object, it knows by the required class name that it's the right shape. The upside is its provably correct by the type system. The downsides are you *have* to give the type a name and you can't assign `Foo` to `Bar` even if they are *identical* interfaces, you're forced to write some kind of adapter. In a larger codebase this is a non-trivial problem.
      TS uses structural subtyping. If it's the right 'shape' its allowed, it doesn't matter what the nominal name is... if it even has one. The upside is you get 99% of the benefits of nominal types without the headaches. The downside is you can now pass a `Gun` to a function that takes a `Camera` because it contains a `shoot()` function. In practice, this almost never happens.
      To be clear, this is all happening at compile time, not runtime, so its distinctly different from 'duck typing'.

  • @kakashi_69_xd
    @kakashi_69_xd 8 หลายเดือนก่อน +1

    Promise solves everything..xd

  • @cherubin7th
    @cherubin7th 8 หลายเดือนก่อน +2

    I don't like that it matches on a literal field inside this objects. Instead the match/switch should check on the type, like you do with an Rust enum.

    • @mikel3377
      @mikel3377 8 หลายเดือนก่อน

      You can't check a type at runtime because types don't exist at runtime in JS/TS.

  • @nomadshiba
    @nomadshiba 8 หลายเดือนก่อน +1

    for type organization, you can also do this:
    export type LoadingState = LoadingState.Loading | LoadingState.Success | LoadingState.Error
    export namespace LoadingState
    {
    export type Loading = {
    status: "loading"
    }
    export type Success = {
    status: "success"
    coords: { lat: number, lon: number }
    }
    export type Error = {
    status: "error"
    error: { message: string }
    }
    }
    don't use `namespace`s for your runtime code, you can't tree shake them.
    but its completely ok to use them for types only

  • @trevydouglas4145
    @trevydouglas4145 8 หลายเดือนก่อน

    noted, but why not just separate the states in one overhead type?
    type LocationState = {
    success:boolean
    error:boolean
    coordinates: {some object}
    errorMessage: ' '
    }

    • @brunokotesky5965
      @brunokotesky5965 8 หลายเดือนก่อน

      You might not have control over the 'state' object you're working with.

    • @maelstrom57
      @maelstrom57 8 หลายเดือนก่อน

      Why would you have a coordinate property at all when you're not in a success state? It's literally the whole of a discriminated union type.

  • @sam_patrik
    @sam_patrik 8 หลายเดือนก่อน

    I'm already using this without knowing its called ' discriminating union '😅

  • @chrishabgood8900
    @chrishabgood8900 8 หลายเดือนก่อน

    separation of concerns.

  • @Book2997
    @Book2997 2 หลายเดือนก่อน

    Hays It adds more lines in my file

  • @CoryTheSimmons
    @CoryTheSimmons 8 หลายเดือนก่อน

    At this point, should we all just migrate to XState? Isn't this basically what it does?

  • @cuongnguyenhuu2836
    @cuongnguyenhuu2836 8 หลายเดือนก่อน

    but u cant reuse the properties in this case

  • @muncho404
    @muncho404 8 หลายเดือนก่อน

    TS is "wannabe Swift" version of JS

  • @utkarshagrawal3640
    @utkarshagrawal3640 8 หลายเดือนก่อน

    Who is still procrastinating their dream project.

  • @brunokotesky5965
    @brunokotesky5965 8 หลายเดือนก่อน +1

    You can work with non literal types, you just need to write a boilerplate type assertion function like this:
    type BaseState = {
    state: string;
    }
    type ExampleState = {
    state: number;
    label: string;
    }
    type States = BaseState | ExampleState;
    function assertion(obj: States): obj is ExampleState {
    return typeof obj.state === 'number' && 'label' in obj;
    }
    let data: any = {
    state: 1,
    label: 'test'
    }
    if (assertion(data))
    data.state // number

    • @maelstrom57
      @maelstrom57 8 หลายเดือนก่อน

      Which is a pain in the ass to do when you have multiple complex types.

  • @user-fu2kg4ug2q
    @user-fu2kg4ug2q 8 หลายเดือนก่อน

    Υοur channel keeps discussing things that are useful for interns. Maybe bump up the difficulty a little bit?

  • @GermanoLisboa
    @GermanoLisboa 8 หลายเดือนก่อน +1

    I love how typescript involves adding a lot of extra code, just to tell typescript that your JavaScript is, indeed, valid code and it should stop complaining

  • @LiRaye
    @LiRaye 8 หลายเดือนก่อน

    The problem is, no api documents something like that 😢

  • @naterpotatoers
    @naterpotatoers 8 หลายเดือนก่อน

    I have some code i need to refactor…

  • @sigma_z
    @sigma_z 8 หลายเดือนก่อน

    As a Brain Fuck developer, I can confirm that I use discrimination union all the time. 😅

  • @tothestars3958
    @tothestars3958 8 หลายเดือนก่อน

    First?

  • @learningjoe1823
    @learningjoe1823 8 หลายเดือนก่อน +1

    not replying to my mail anymore 😢

    • @adityaanuragi6916
      @adityaanuragi6916 8 หลายเดือนก่อน

      Out of topic
      But his mail weekly updates aren't as consistent anymore I enjoyed reading em a lot!!

    • @WebDevSimplified
      @WebDevSimplified  8 หลายเดือนก่อน +2

      They will become more consistent soon. I was crazy busy over the last few months and the newsletter/blog was the thing I had to cut to keep up with other stuff. Now that I am back to a more normal workload I should be more consistent with weekly/bi-weekly updates/articles.

  • @93kwaj
    @93kwaj 8 หลายเดือนก่อน

    vvvvvvvvvvvvvvvvvvv

  • @T1Oracle
    @T1Oracle 8 หลายเดือนก่อน +1

    In Rust you just use an enum and give each enum the appropriate properties. This mess would never even come up. I really want to stop using JS. It's claimed over two decades of my life already.