any vs unknown vs never: TypeScript demystified

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ย. 2024

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

  • @kugi7786
    @kugi7786 ปีที่แล้ว +16

    you going over the parts of the language that i didn't know is a huge help, love your videos

  • @balaganesh3440
    @balaganesh3440 ปีที่แล้ว +5

    That was the most clear and simple explanation of never, I've ever come across. You've really demystified it!

  • @merotuts9819
    @merotuts9819 ปีที่แล้ว +4

    These types mixed with Set theory makes great sense. Thanks Andrew.

  • @javo2432
    @javo2432 ปีที่แล้ว +1

    this video popped out on my recommendation but i watched the whole thing thanks

  • @rlkandela
    @rlkandela ปีที่แล้ว

    The exhaustive switch is amazing

  • @mbossaful
    @mbossaful 4 หลายเดือนก่อน

    Great video! Another use for the 'never' type is as the return type of functions that will always throw (e.g. exception helper functions). This lets the IDE detect any code after the function call as unreachable.

  • @ДилишШадманов
    @ДилишШадманов ปีที่แล้ว

    The simple and understandable explanation i have ever seen about typescript types

  • @helsontaveras7961
    @helsontaveras7961 ปีที่แล้ว +1

    I will like every video you post because they’re all amazing.

  • @Eitrii
    @Eitrii ปีที่แล้ว

    Your explanation just gave me the 'ooOOOooohh!' moment. Liked and subscribed

  • @ajgubi19
    @ajgubi19 ปีที่แล้ว

    Hands down the best explanation of these three keywords. Loved it! Understood it for the very first time. Thank you! Subscription already paying off!

  • @_briantravis
    @_briantravis 11 หลายเดือนก่อน

    amazingly clear explanation, subscribed!

  • @dinckelman
    @dinckelman ปีที่แล้ว +3

    Unknown is up there on the godtier list along with the generics

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

    Thank you very much. very useful lesson. Thank you for sharing 🙏

  • @JakeAndDaddy
    @JakeAndDaddy ปีที่แล้ว +1

    I’m a hard man to please when it comes to developed TH-cam channels.
    I’ve seen it and done it all.
    I’ve got to say though that your content and the way it’s presented is vastly superior to a lot of channels that had exponential growth.

  • @ivaylopetrov9956
    @ivaylopetrov9956 ปีที่แล้ว

    Thank you, man! SetTheory rules 💪

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

    Nice explanation

  • @joel_bio
    @joel_bio 6 หลายเดือนก่อน

    Fantastic perspective

  • @Luxcium
    @Luxcium ปีที่แล้ว

    The colour grading of those video makes me so happy, the iconic face of Andrew even more and the quintessence of all that being about typescript is making me feel so privilege and lucky to exist in a univers where it is possible to witness any of this

    • @andrew-burgess
      @andrew-burgess  ปีที่แล้ว

      Ha, you're too kind. Colour Grading === whatever comes out of my iPhone.

    • @Luxcium
      @Luxcium ปีที่แล้ว

      @@andrew-burgess you are filming this with your phone 📱 wow 🤩

  • @viridianite
    @viridianite ปีที่แล้ว

    You're killing it with these videos, Andrew! I think what many tech videos about languages miss is pragmatism and usage, after all anyone can read the docs and figure out syntax. However you've found a balance between theory and pragmatism.

  • @ankitputhran
    @ankitputhran ปีที่แล้ว

    Wonderful explanation

  • @FunctionGermany
    @FunctionGermany ปีที่แล้ว +3

    3:52 i would have mentioned that you probably wouldn't want to write a lot of this kind of manual object parsing code. it gets very messy and you're probably best off using something like zod to parse unknown objects.

    • @andrew-burgess
      @andrew-burgess  ปีที่แล้ว

      Oh yeah, absolutely! I just wanted to show how to narrow from unknown to something more concrete. But I’d probably only use this approach for primitives.

  • @vOnez212
    @vOnez212 9 หลายเดือนก่อน

    Great explanation!

  • @nielsgregersen1351
    @nielsgregersen1351 ปีที่แล้ว +6

    I assume that Record means that the type could also be Record ?

    • @andrew-burgess
      @andrew-burgess  ปีที่แล้ว

      Hmm, maybe? Not sure why though, because I already validated that foobar is a number.

    • @nielsgregersen1351
      @nielsgregersen1351 ปีที่แล้ว

      @@andrew-burgess You validated that what was assigned to it at runtime was a number
      let a :string | number = 42;
      if ( typeof a === 'number') {
      console.log(a)
      }

    • @yitzchaksviridyuk932
      @yitzchaksviridyuk932 ปีที่แล้ว

      @@andrew-burgess This is what ChatGPT says:
      "The reason why TypeScript still shows val.foobar as unknown inside the if block is due to a limitation in the current version of TypeScript's control flow analysis. Although TypeScript is able to narrow down the type of val based on the if statement condition, it is not yet able to use this information to refine the type of val.foobar to number. Instead, TypeScript conservatively treats val.foobar as unknown to ensure that the code is type-safe."
      So there you go, one day we'll get there. 😂
      Btw, I also checked why Typescript allows us to assign 5 to val.foobar if val.foobar is considered unknown. ChatGPT said that Typescript is performing implicit type assertion in this case.

  • @ΣτάθηςΣταθόπουλος-σ7ρ
    @ΣτάθηςΣταθόπουλος-σ7ρ 5 หลายเดือนก่อน +1

    At 4:30, that got fixed at version 5.5.

  • @JohnWasinger
    @JohnWasinger ปีที่แล้ว

    I bet you never thought you’d fall this deep into your tutorial watching journey.

  • @Luxcium
    @Luxcium ปีที่แล้ว

    So you have me who is passionate about both this content creator and the language *TypeScript* watching a video about something that became somehow really trivial (It was not at the beginning do not worry if you are new to TypeScript and trying to understand *any* can *never* be trivial while it is *unknown* )...

  • @fallingseasy
    @fallingseasy ปีที่แล้ว

    Thanks! Great video

  • @dawid_dahl
    @dawid_dahl ปีที่แล้ว

    5:20 ”That type of thing.”
    I saw that you did there.

  • @tech_channel110
    @tech_channel110 6 หลายเดือนก่อน

    well defined

  • @RozbehSharahi
    @RozbehSharahi 10 หลายเดือนก่อน

    The record example might be misleading.
    In the record example the reason for Record is following: You just verified that foobar is currently a number, but there is no definition that foobar _must_ be a number. So within your if, you can do number operations like "toFixed", but nobody is prohibiting you to write a string or anything else on it (no-type-declaration).

    • @RozbehSharahi
      @RozbehSharahi 10 หลายเดือนก่อน

      To solve this you'd need to create another object Object-B with clear type-definitions and pass the values to it. This way you define that Object-B must have foobar as number.

  • @EnzoAuditore
    @EnzoAuditore 10 หลายเดือนก่อน

    Thanks mate

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

    Another use of "never" is the return type for functions that always throw an error.

  • @ayushdixit8993
    @ayushdixit8993 11 หลายเดือนก่อน

    Thanks Man....

  • @clasesutnfrc8699
    @clasesutnfrc8699 ปีที่แล้ว

    02:29 How should we use the unknown type?

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

    Great video. Lets say val is unknown and an object. How to get all components of the unknown object? So instead of checking if foobar in val, I want to get a list of all available components and then check the values of those components.

  • @juanmiguel431
    @juanmiguel431 ปีที่แล้ว

    But I think you can check the type of a variable defined as any type.

  • @deatho0ne587
    @deatho0ne587 ปีที่แล้ว

    I just wish there was no production code that had the any type. I get it, but man are certain things hard to narrow down. And tryting to add strict mode to the whole code base is just going to make the Bussiness not happy for the tech debt it will incure.

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

    Now help me make use of Unknown type when i want to say that this unknown function argument is of type of my custom interface. How do i do that ?

  • @Luxcium
    @Luxcium ปีที่แล้ว

    any is the set of all set a cursed concept that is analogous to the nonexistence of both the type checker and set of all set then unknown is the set of all type... never the empty set...

  • @siya.abc123
    @siya.abc123 ปีที่แล้ว

    I have to subscribe now, been coming across your clear and to the point clips for a while but somehow I didn't subscribe

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

    Yeah it sends money.

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

    I like never!

  • @soniablanche5672
    @soniablanche5672 ปีที่แล้ว

    void vs undefined

    • @enzodossantos2546
      @enzodossantos2546 ปีที่แล้ว

      Void: nothing to return. Undefined: create a variable and don't assign a value.

    • @soniablanche5672
      @soniablanche5672 ปีที่แล้ว

      @@enzodossantos2546 It's confusing because in JavaScript when you return nothing you are actually returning undefined

  • @ericb314
    @ericb314 6 หลายเดือนก่อน

    type TODO = any;