Infer is easier than you think

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 มิ.ย. 2024
  • TypeScript's infer keyword is MUCH narrower, much less widely used than you think it is. It has ONE use case, and we cover it in a ridiculous amount of depth here.
    Become a TypeScript Wizard with Matt's upcoming TypeScript Course:
    www.totaltypescript.com/
    Follow Matt on Twitter
    / mattpocockuk
  • ภาพยนตร์และแอนิเมชัน

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

  • @nyambe
    @nyambe ปีที่แล้ว +118

    You really are the Typescript wizard.

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

      Indeed he is

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

      Harry

    • @johnyepthomi892
      @johnyepthomi892 11 หลายเดือนก่อน +3

      He filters out BS and get to the point in the clearest way possible. Communication is key.

  • @jeralm
    @jeralm ปีที่แล้ว +76

    I already considered myself an advanced TypeScript dev, but a good understanding of infer was missing from my arsenal. Thank you so much for this.

  • @dueft4479
    @dueft4479 ปีที่แล้ว +20

    The comparison with the replace-regex was awesome. Helped me alot to understand infer better! Thx Matt!

  • @ivan_dramaliev
    @ivan_dramaliev ปีที่แล้ว +35

    To me, the most intuitive mental model for types (in TypeScript) is that of (mathematical) sets. 'never' is simply the empty set, 'any' is the set of everything, 'string' is the infinite set of all possible strings, while any particular string (e.g. 'abc') when used as a type is simply the set consisting of a single element that is that particular string, and so on. Generics act somewhat like functions at the level of the type system (with the types listed between < and > acting as the arguments), and the 'extends' and 'infer' operators are there for type pattern matching and extraction.

    • @user-hd7ju4wu4b
      @user-hd7ju4wu4b 10 หลายเดือนก่อน +2

      Came to say this, you are very right

    • @Rostgnom
      @Rostgnom 9 หลายเดือนก่อน +3

      What's the mathematical anology to the `unknown` type?

    • @user-hd7ju4wu4b
      @user-hd7ju4wu4b 9 หลายเดือนก่อน +3

      @@Rostgnom i see it as a set that contains all the other sets

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

      What's the 'never' type? Something you haven't put into a set yet?

    • @user-hd7ju4wu4b
      @user-hd7ju4wu4b 9 หลายเดือนก่อน +1

      @@Alastairtheduke1 just empty set with no elements

  • @jasonstewart7983
    @jasonstewart7983 ปีที่แล้ว +11

    Very cleanly spoken. The mental model for 'infer' is super valuable. The comparison of 'extends' to a regex match and 'infer' to the capture group of the regex was what I was missing. It's so much easier to get my head around this after watching this video. Three cheers!

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

    I was happy and satisfied with the basic pattern matching mental model of infer, then you completely blew my mind when you went even deeper into infer black magic and completely demystified it. Well done!

  • @WilderPoo
    @WilderPoo ปีที่แล้ว +11

    I love that this is 13:37 long

    • @mattpocockuk
      @mattpocockuk  ปีที่แล้ว +8

      Gotta go deep

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

      the longer an explanation is the more shallow it gets!@@mattpocockuk

  • @MrMcFyfey
    @MrMcFyfey ปีที่แล้ว +7

    Great explanation, thanks Matt! I've been trying to find some more advanced typescript tutorials for a while now

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

    Came here after seeing the infer keyword in the ReturnType generic type definition wondering what this magic was.
    Thanks for the explanation.

  • @samerkayali4525
    @samerkayali4525 ปีที่แล้ว +2

    You just made infer so easy! it was a thing that I have struggle to understand until seeing this video! Matt thanks a lot! You are a true hero and TypeScript mage!

  • @darrenvong9404
    @darrenvong9404 ปีที่แล้ว +2

    Even though I have an idea of what infer does, I still learnt something new from this (didn't know it's only applicable to conditional types)! Can't wait to dig into Total TypeScript more as you expand the content in that course!

  • @edgeeffect
    @edgeeffect ปีที่แล้ว +2

    I'm a recent "convert" to your channel. There's a lot of (let's face it) simply awful programming tutorial channels on TH-cam and it's such a rare treat to find one, like this, that's of really good quality. It's got to the stage now where you're knocking "Arjan Codes" of of my personal number 1 spot. Keep up the excellent work.

  • @user-mf1ft6iz4l
    @user-mf1ft6iz4l ปีที่แล้ว +1

    He explained everything in a very simple and easy to understand way. thank you

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

    Been working on some typesafe express middleware lately, and infer really did a number on me. This cleared things up heaps, thanks for this!

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

    This is definitely the missing explanation for "infer" that I was needing. Great job, can't wait to see more content.

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

    I gotta say that your videos are very intimidating for me because I'm still at an intermediate level in JS Bootcamp but your calm style and clear explanations make me kinda understand much of what you expose in your videos once I watch them a couple of times. I can't wait to reach a level where I'm ready to buy your TS course. Thank you, great work!

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

    Incredible. I grok'd the official docs explanation and it helped me understand some parts of a project codebase that I was struggling with before. But after watching this video, it's absolutely crystal clear and gives me much more context for how it can be used more broadly

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

    The regex analogue really helps paint the mental model, great video!

  • @peachesfruitella
    @peachesfruitella ปีที่แล้ว +2

    Liked the pairing with ‘never’ explanation, and how to think about its usage, felt like an aha moment 🎉

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

    Love your style of teaching.

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

    i liked because you are awesome, your explanations, the energy, the pedagogie, a true wizard legend

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

    Thank you Matt, your explanation really help me understand Typescript better. Now I'm also enjoy doing Type Chalenge because of it.

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

    Underrated Master of Typescript 👌

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

    Thank you very much! I am a Ruby developer who is interested in TS and your videos really help me dive deep into TS.

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

    Genuinely thought I was subscribed until the shout out at the end, now I am! Great vid

  • @NedCollyer
    @NedCollyer ปีที่แล้ว +2

    Thanks. Perfect :) Infer was the missing piece of the puzzle for me. I sorta understood what it was doing, but the syntax was just a bit nuts with the all the "extends matching".

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

    Very insightful! Thank you.

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

    This is brilliantly explained. Thank you Matt! I got my employer to buy Total TypeScript for me, can't wait to dig in!

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

    This is exactly what I was looking for, thank you

  • @ljuglampa
    @ljuglampa ปีที่แล้ว +41

    You might be a TypeScript wizard, not sure, but I do know you're a educational wizard! Thank you for doing these ❤️

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

    Amazing video! Thank you so much for this super clear and helpful explanation :)

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

    Love your videos! Sharing with my team

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

    A brilliant explanation!

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

    Thanks Matt!!

  • @user-cp7fl1jl5e
    @user-cp7fl1jl5e ปีที่แล้ว

    Awesome explanation! Easy to digest

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

    Best explanation ever. Thank you

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

    Loved the video. You're a gem

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

    Great explanation, thanks!

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

    I have to apply this knowledge somehow into my head now. That was great!

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

    Thank you very much for the channel Matt aka TS Wizard 🙏. Just want to mention that something about the starting explanation what is conditional type and basically constrain in TS. So I understand it as the construction 'A extends B' basically means A subset of B. From SetTheory in Maths. I saw that many people are replaying this part, including me.
    Happy coding 💪

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

    The object examples made it clear to me :D Thanks

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

    You are so good at teaching 😍 thanks for sharing your knowledge

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

    explained brilliantly

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

    Now this. This is extremely useful.

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

    Ahh ok. I don't think I fully grasped the potential of `infer` until you brought in the bit about being able to confer constraints on it with the extends keyword. Really cool stuff

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

    Really, really awesome explanation!

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

    You have my respect kind sir.

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

    I totally love it! finally, I understand these crazy extends I was thinking of it as a extending some parent class or something It was driving me crazy

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

    great content man keep it up 💪🏼

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

    Thank you, you are amazing!

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

    Great content, Matt 😜

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

    great explanation

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

    Great video ❤, regards from México

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

    Unfortunately, despite its popularity, Typescript has very poor documentation. Very often I don't understand what Typescript can or can't do.
    Thanks Matt for explaining those tricky parts.

  • @bartek...
    @bartek... ปีที่แล้ว

    Video length is epic!!!

  • @AhsanKhan-eb2zb
    @AhsanKhan-eb2zb ปีที่แล้ว

    great explanation

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

    Dude You are one of the best web-dev related channels on YT, and You only have a bit more than 20k followers... Hope Your channel will grow because You definitely deserve it! Buying Your paid course! :D

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

      Thanks pal! Gained 11k subs in 3 weeks so hoping that changes soon!

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

    This is gem

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

    Found you from Theo. You’re awesome Matt! ❤ 💻

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

    Lovely thanks

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

    wow amazing! thanks!

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

    Best explanation

  • @NoOne-ev3jn
    @NoOne-ev3jn ปีที่แล้ว

    I just found a gem kind of channels

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

    I liked it. Literally and metaphorically

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

    I am your fan typescript wizard ❤️

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

    Really helpful! Thank you!
    The only complaint is the jumping between examples (going down then up the file), makes it harder to follow

  • @-anonim-3008
    @-anonim-3008 27 วันที่ผ่านมา

    Thanks a lot! That's easy)

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

    Que bien explicado

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

    Could you make a video explain peer dependencies like a TypeScript Wizard?

  • @btiwari-games5279
    @btiwari-games5279 ปีที่แล้ว

    I'll like to tell my friends about you as that typescript guy

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

    Request: Please plan your examples ahead of time. After making each change, take some time to explain it. No need to rush. Thanks for your videos. They are super helpful.

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

    Great pedagogy

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

    Great video Matt! Is it also possible to infer the type of function parameters? What's the correct way?

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

      Yes!
      export type Parameters = T extends (...args: infer TParameters) => any ? TParameters : never;

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

    My man

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

    Just another quite not so easy thing made simple. infer is really useful and super fun!

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

    I believe never should be nothing and unknown should be something in typescript.

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

    love how he said boolean with french style :)) funny teacher

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

    Why not use regex backreferences ($1) here as an analogy?

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

    Combine composition with infer keyword and you will get unimaginable solutions.

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

    my mind exploded somehere near 5 minute, but i survived until the end and remember one crucial thing: about the only use case of infer. Did i hate the video? I made it until the end so i give a thumb up.

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

    The fact that the length of this video is 13:37 🤯

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

    Hi Matt, when will the course be available for purchase? What bonuses do I lose if I use the regional price?

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

      Hey pal, we'll put it back on sale in January once we've got the second module ready. We are planning some bonus content, but we haven't released info about it yet. If you buy it via PPP we can always upgrade you if needed.

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

      @@mattpocockuk sounds good, thanks for the answer

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

    manager: when a user clicks this button, please increase the counter in the db
    web devs af:

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

    Infer is like extracting a type as a variable. As soon as I understand that, Infer was easy.

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

    what I learned was, do
    type AntiArray = T extends (infer U)[] ? U : never;
    not
    type AntiArray = T extends unknown[] ? T[0] : never;

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

    What happens when you have a union of infers and the object being inferred from matches more than one pattern? Does it follow the first pattern, last one or a union of all?

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

    Hello TS Wizard!

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

    Good God, I've been declaring so many different generic arguments with default values when I should have been using infer

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

    i should like it and i do like it🖤

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

    This went by a little fast. But very interesting!!!

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

    Can you tell me the font-family you used here ?

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

    well.. subscribed :D

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

    Video duration is leet!

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

    Any tips on how to use infer to get the type of an array item?
    I have types being generated from swagger
    This comes from the generator:
    type Thing = {
    org_key: string;
    role: "viewer" | "editor" | "admin";
    }[] | undefined
    Trying to do type Blah = Thing[0] doesn't work because ofthe | undefined.
    Any help would be greatly appreciated!

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

    I might be wrong but is infer basically acting like a tag that says tells ts to pay attention to something. Which we can reuse for more things?

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

    So infer keyword allows you to grab the type of the thing that was passed and use it later as if it’s a variable.

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

    I've seen infer used in a really cool type that replaces parts of strings in a template literal within a type. I still can't really understand how it works there even after watching this.

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

      The type looks like this:
      export type Replace = T extends `${infer L}${S}${infer R}` ?
      Replace : `${A}${T}`

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

    I didn't understand anything. 😅😅 what are the prerequisites to understand this?

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

    i think the problem with infer is you dont use it that often so you dont remember about it that much and in the places you think you can use it, you find out that you cant use it, or make it work etc
    so for example i think
    also syntax is also confusing,
    instead of this:
    type Last = T extends [...any, infer L] ? L : never
    this would have made more sense:
    type Last = T extends [...any, any as L] ? L : never
    or keyword should be change to something like, let, which makes more sense
    type Last = T extends [...any, let L extends any] ? L : never

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

    Hi, my request may be peculiar but, could you make a video where you show how typescript utility types are used in real life situations because the only time I need to use them is within type-challenges (kinda like a bubble). So is it only me or many people feel this ?