Infer is easier than you think

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.พ. 2025

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

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

    You really are the Typescript wizard.

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

      Indeed he is

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

      Harry

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

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

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

    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.

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

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

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

    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 ปีที่แล้ว +5

    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!

  • @codecleric4972
    @codecleric4972 10 หลายเดือนก่อน +1

    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

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

    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.

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

    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.

    • @ІлляМіхневич
      @ІлляМіхневич ปีที่แล้ว +2

      Came to say this, you are very right

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

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

    • @ІлляМіхневич
      @ІлляМіхневич ปีที่แล้ว +3

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

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

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

    • @ІлляМіхневич
      @ІлляМіхневич ปีที่แล้ว +1

      @@Alastairtheduke1 just empty set with no elements

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

    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!

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

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

  • @darrenvong9404
    @darrenvong9404 2 ปีที่แล้ว +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!

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

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

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

    Underrated Master of Typescript 👌

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

    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!

  • @윤지만-w4m
    @윤지만-w4m 2 ปีที่แล้ว +1

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

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

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

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

    I love that this is 13:37 long

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

      Gotta go deep

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

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

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

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

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

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

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

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

  • @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

  • @grzesieksgs
    @grzesieksgs 2 ปีที่แล้ว +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  2 ปีที่แล้ว

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

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

    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 💪

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

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

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

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

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

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

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

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

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

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

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

    Love your style of teaching.

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

    Thanks so much for this amazing video Matt!
    You really helped me solidify my understanding of infer.
    One thing I have been struggling with is inferring generic function parameters.
    If you take this map function, T and U are inferred as unknown:
    ```ts
    type MapFunction = (mapper: (val: T) => U, list: T[]) => U[]
    type MapParameters = Parameters // [mapper: (val: unknown) => unknown, list: unknown[]]
    ```
    This makes sense given T and U have no constraints.
    If you added constraints such as `` then the mapper `val` and `list` types would be of type `string` and the mapper return type would be of type `number`
    However a mapping function should remain generic and what I really want/need here is:
    ```ts
    type MapParameters = Parameters // [mapper: (val: T) => U, list: T[]]
    ```
    This would bind the relationship between the mapper val argument and the list type.
    The relationship between the mapper val argument and the list are decoupled with the unknown type.
    For arguments sake, let's say I defined PartialFunction type:
    ```ts
    // Implementation 1
    type PartialFunction1 = F extends (...args: infer A) => infer R
    ? (...args: Partial) => R
    : never
    // Implementation 2
    type PartialFunction2 any> = (
    ...args: Partial
    ) => ReturnType
    ```
    Both of these PartialFunction types are the same (or so I believe) and both create the following type for the MapFunction defined above:
    ```ts
    type PartialMap1 = PartialFunction1
    type PartialMap2 = PartialFunction2
    // PartialMap1 = (mapper?: ((val: unknown) => unknown) | undefined, list?: unknown[] | undefined) => unknown[]
    // PartialMap2 = (mapper?: ((val: unknown) => unknown) | undefined, list?: unknown[] | undefined) => unknown[]
    ```
    Since the params of the generic MapFunction are being inferred as unknown and then spread onto the new function, the generic constraint between the mapper function and the list is lost.
    Is there anyway that you know of to solve for this or is this a current limitation of TypeScript?
    Here's a link to TypeScript playground with the types shared above:
    www.typescriptlang.org/play/?#code/C4TwDgpgBAsghmAYgVwHYGNgEsD2qoC8UAPACoA0UAqgHwAUAtgpAE4BcUdAbnADYekAlIRrVKvLAGdgAgNoBdYQVFUFAKFCRYCAApwWcBhGAQWkwlD0GjJs8XhI0mXKlEB6N1FlMwrDtz4ONABrVBwAd1QlURCwyPEpGShYiNQFeTUNcGgrbD4UDGw8AEZiRFEiRCgIAA8TVAATczoAOjb9AHNJDixUADNTKABBaKhegZYoACU1KCgAfk42ls7uy3083mIhmlGZuY5UCC5TTM0cjax8pyLUACYy6rqIRubl1Y44VBAFUa+QCqcWZQd4sLocXJXLZWQzGUySMo0GhqPbGZAsVCkbKIs7ZdYsTYOYoWSHXQouUoOArOPDuTyMZimeb+AL8ZKoUKpUYpSLCAA+7IaED6vQgDQS0mZ7M5kQUUAFaCFIqODW5HLiaQy53xhIQdxJlzJNPu9gQ1NudM4PlYUrorKC6q5ImlGv5guFovFUAkkodMs18vdyrFav96iAA

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

    This is exactly what I was looking for, thank you

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

    Now this. This is extremely useful.

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

    Love your videos! Sharing with my team

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

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

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

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

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

    The object examples made it clear to me :D Thanks

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

    Very insightful! Thank you.

  • @梁永霖-o6u
    @梁永霖-o6u ปีที่แล้ว

    Awesome explanation! Easy to digest

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

    Loved the video. You're a gem

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

    You're amazing, man! Thank you!

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

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

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

    Best explanation ever. Thank you

  • @softgripper
    @softgripper 2 ปีที่แล้ว +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".

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

    A brilliant explanation!

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

    explained brilliantly

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

    You have my respect kind sir.

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

    Thanks Matt!!

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

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

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

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

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

    Video length is epic!!!

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

    Great content, Matt 😜

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

    Great explanation, thanks!

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

    Really, really awesome explanation!

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

    Great video ❤, regards from México

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

    I just found a gem kind of channels

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

    great explanation

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

    Love this. Thanks

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

    great content man keep it up 💪🏼

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

    Thank you, you are amazing!

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

    This is gem

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

    Best explanation

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

    I liked it. Literally and metaphorically

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

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

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

    great explanation

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

    I am your fan typescript wizard ❤️

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

    wow amazing! thanks!

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

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

  • @justMeJustBri
    @justMeJustBri 2 ปีที่แล้ว +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!

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

      type Blah = NonNullable;

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

    Lovely thanks

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

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

  • @andsheterliak
    @andsheterliak 2 ปีที่แล้ว +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.

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

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

  • @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.

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

    2:25 just one question. Why not explicitly define the return type of your function in first place ao that u wont even need nor infer, nor returntype helper?
    (): string=> ...

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

      ReturnType is useful for functions you don't control, like in third-party libraries, that don't export their types.

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

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

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

    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 8 หลายเดือนก่อน

    Thanks a lot! That's easy)

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

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

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

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

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

    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 2 ปีที่แล้ว

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

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

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

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

    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?

  • @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;

  • @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.

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

    My man

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

    Great pedagogy

  • @ashleyjbartlett
    @ashleyjbartlett 2 ปีที่แล้ว

    it took me a most of this video to realise the ternary statement was for the extends, not the infer. Ah hah.

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

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

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

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

  • @czlowiek48881
    @czlowiek48881 2 ปีที่แล้ว

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

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

    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 2 ปีที่แล้ว

    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.

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

    So, infer is like typeof but for conditional types?

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

      Think of it more like a capture in a regex

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

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

    • @mattpocockuk
      @mattpocockuk  2 ปีที่แล้ว +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 2 ปีที่แล้ว

      @@mattpocockuk sounds good, thanks for the answer

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

    Que bien explicado

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

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

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

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

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

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

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

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

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

    Hello TS Wizard!

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

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