4 ways to use the TypeScript infer keyword

Поделиться
HTML-код
  • Опубликовано: 27 май 2024
  • I've always been a little baffled by TypeScript's `infer` keyword? So I explored the ways you can use it, and came up with 4 variations! If you have other fun examples of how to use `infer`, let me know in the comments!
    0:00 - Intro
    0:32 - Generic Arguments
    2:25 - Arrays
    4:37 - Function Parameters and Return type
    7:03 - Template Literals
    9:53 - Outro
  • НаукаНаука

Комментарии • 46

  • @stonecoldcold2941
    @stonecoldcold2941 Год назад +7

    I've always never understood infer. But the examples you have shown is the best that made me understand finally.

  • @dimitargetsov9690
    @dimitargetsov9690 Год назад

    Two reasons to remember and share this channel with anybody:
    1.content (on TS) second to none.
    2.family name -Burgess--same as of Antony Burgess--"Clockwork Orange".

  • @user-vp7ht2fg1u
    @user-vp7ht2fg1u Год назад

    That is the best explanation of infer ever.
    It is not fair.
    Why shitty stuff like "BECOME A FRONTEND DEVELOPER IN 5 SECONDS" is much more popular...

  • @serglohm
    @serglohm 2 года назад

    Great job! This is definitely the best video about infer on 2022!

  • @devinorium
    @devinorium 9 месяцев назад

    Upped the production quality. Looks dope

  • @vihanv8582
    @vihanv8582 2 года назад +3

    Wow! This has got to be the best video on the infer keyword I've ever seen. Keep up the good work Andrew! Liked and subbed 😀

  • @BobbyBob21
    @BobbyBob21 Год назад

    Thank you. Perfect examples.

  • @user-zx8js8do5b
    @user-zx8js8do5b 2 года назад +1

    What a great explanation, thank you so much!

  • @AiguretDuren
    @AiguretDuren 11 месяцев назад

    This was legitimately fun to code along with. Thank you!

  • @i.j.5513
    @i.j.5513 Год назад

    Really nice demonstrations. Thanks a lot! It really made me understand the infer keyword better!

  • @vishwajeetraj11
    @vishwajeetraj11 Год назад

    This was Epix.
    Looking out for more.

  • @user-pk8dz4qc9b
    @user-pk8dz4qc9b Год назад

    This was the best explanation for infer. Cheers!

  • @ffedchik
    @ffedchik 2 года назад

    Awesome content. Keep up the great work!

  • @the_proletariat
    @the_proletariat 5 месяцев назад

    This was cool. I didn't know anything about infer. Thanks to your video and challenges, now I know. Keep up the good work!

  • @johnyepthomi892
    @johnyepthomi892 Год назад

    Dayum. This is very useful. Thanks for sharing.

  • @joshreynolds4164
    @joshreynolds4164 Год назад

    Great explanation on this. Infer is so powerful

  • @mohibwasay5102
    @mohibwasay5102 2 года назад

    Wow. Awesome. Thanks Andrew.

  • @developerfriendly
    @developerfriendly Год назад

    superb tutorial!

  • @gangster4208
    @gangster4208 Год назад

    Thanks for the video!

  • @kyuubyemy
    @kyuubyemy Год назад +1

    My implementation of FirstParam and SecondParam:
    type FirstParameter any> = T extends (firstParam: infer A, ...args: any[]) => any ? A: never;
    type SecondParam any> = T extends (firstParam: any, secondParam: infer A, ...args: any[]) => any ? A: never;
    Also Jojo is everywhere LMAO

  • @yujitomita3969
    @yujitomita3969 2 года назад

    Thank you for this. Agreed with Jimmy this is hard.

  • @chamir4614
    @chamir4614 2 года назад

    Great video dude!! Thx you :D

  • @mikedelcastillo
    @mikedelcastillo Год назад

    you are a god kind sir, thank you!

  • @ChesterRivas
    @ChesterRivas Год назад

    This is good stuff.

  • @samirnayibruza9135
    @samirnayibruza9135 7 месяцев назад

    You could do something like this instead of using overloads on functions:
    ```ts
    interface Item {
    id: number
    }
    interface FindOptions {
    one: boolean
    }
    declare function find(options: Options): Options extends {one: infer FindOne} ? FindOne extends true ? Item: Item[] : never
    const item = find({one: true}) // Item
    const items = find({one: false}) // Item[]
    ```
    Edit:
    After a few minutes I realized that in the above case there is no need because I could just do `Options extends {one: true}` directly but imagine the possibilities 😁

  • @edgarabgaryan8989
    @edgarabgaryan8989 2 года назад

    awesome!

  • @shayanpaul3430
    @shayanpaul3430 Год назад +1

    I have a question related to testing whether two types are equal or not. The utility type in @type-challenge is as follows :
    export type Equals =
    (() => T extends X ? 1 : 2) extends
    (() => T extends Y ? 1 : 2) ? true : false;
    Maybe it a very basic or idiotic question but i am unable to understand from where this T type parameter is coming from and what will be its values

    • @andrew-burgess
      @andrew-burgess  Год назад +1

      Definitely not a dumb question, TypeScript's syntax here is pretty confusing. My understand is that, by declaring a generic parameter T (that declaration is the part), you're saying that T should be assigned when you call the function. Like this:
      declare function oneOrTwo(): T extends string ? 1 : 2;
      const x = oneOrTwo(); // 1
      const y = oneOrTwo(); // 2, because T defaults to unknown
      I found this really in-depth explanation of the whole Equals type, which might be helpful too: stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript
      Thanks for watching!

  • @georgejenkins4167
    @georgejenkins4167 Год назад

    Subbed!

  • @MohamedRagab-kp6bp
    @MohamedRagab-kp6bp 2 года назад +2

    type MyParameters<
    T extends (...args: any[]) => any,
    I extends number
    > = T extends (...args: infer A) => any ? A[I] : never;

  • @michaelalls8230
    @michaelalls8230 Год назад

    Tried to use typescript-challenges but getting an error on the import of the challenges utils. Is there something special that has to be done to get that working?

    • @andrew-burgess
      @andrew-burgess  Год назад

      Hmm, don’t think so. Can you comment with a link to your playground?

  • @handsome_man69
    @handsome_man69 Год назад +1

    Handsome man

  • @samislam2746
    @samislam2746 Год назад

    omg, how do you know all that stuff!

  • @afuzzybearsyoutubechannel2812
    @afuzzybearsyoutubechannel2812 Месяц назад

    💚

  • @samirnayibruza9135
    @samirnayibruza9135 7 месяцев назад

    🤯🤯🤯🤯🤯

  • @JeanAlesiagain3
    @JeanAlesiagain3 2 года назад

    Typescript has become very complicated

  • @michaeldimmitt9974
    @michaeldimmitt9974 Год назад

    lol

  • @alsbahsy
    @alsbahsy 2 года назад

    this is harder than rocket science

    • @andrew-burgess
      @andrew-burgess  2 года назад +1

      Nah, you got this! Let me know if you have questions, happy to help!

  • @BHFJohnny
    @BHFJohnny Год назад +1

    Nope. Too advanced. Need to back off a bit

    • @andrew-burgess
      @andrew-burgess  Год назад

      Thanks for checking this out! Here’s one where I explain the details a little finely:
      ruclips.net/video/wON6MCS0NkE/видео.html

  • @AhmedAli-jx9ie
    @AhmedAli-jx9ie 8 месяцев назад

    I didn't understand shit

  • @StuartLoria
    @StuartLoria 7 месяцев назад

    Weirdd explanation