My Favorite TypeScript Tips and Tricks

Поделиться
HTML-код
  • Опубликовано: 21 ноя 2024

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

  • @studiowebselect
    @studiowebselect 9 месяцев назад +4

    type __Awaited = T extends Promise ? U : T
    will work and no recursivity

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

      Good catch - I've always used the recursive snippet, but it's overkill for what I was demonstrating here.

    • @studiowebselect
      @studiowebselect 9 месяцев назад +1

      @LachlanMiller by the way, I learn so mush from you. Love your videos! Continue your great work

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

      Thanks@@studiowebselect ! Trying to get back into a habit of posting content. Knowing people find it useful really motivates me!

    • @MarekVaníček-u1n
      @MarekVaníček-u1n 9 месяцев назад

      What about nested promises tho. Wont work for those

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

      @@MarekVaníček-u1n Yeah, depends what you want. The recursive solution handles the nested cases.
      Here's the actual one from TypeScript:
      gist.github.com/lmiller1990/067b9d296bf5a3b05151437009ad7235

  • @siyedyoussef3202
    @siyedyoussef3202 9 месяцев назад +3

    love this kind of video it open your eyes about some typescript magic keep up the great work

  • @JesseBethke
    @JesseBethke 3 месяца назад

    For the Index Types; what I do is define TestingTypes as an enum and TestingType as a `typeof keyof TestingTypes`

  • @user-ug8qc6tr6b
    @user-ug8qc6tr6b 9 месяцев назад +1

    Great content! keep going

  • @yassinebenazouz4529
    @yassinebenazouz4529 9 месяцев назад +1

    very helpful thank u ^ ^

  • @malcolmvernon6808
    @malcolmvernon6808 4 месяца назад +1

    typescript already gives you warnings if you try to edit a const array or object litteral

    • @LachlanMiller
      @LachlanMiller  4 месяца назад

      Did not realize this - which tip are you referring to w/ this comment specifically?

    • @malcolmvernon6808
      @malcolmvernon6808 4 месяца назад

      @@LachlanMiller In close to the beginning of the video you said to mark const variables "as const" in typescript (first tip actually). And I thought "wow what a cool trick" and then I did an experiment with it and then I was like - WAIT A MIN - typescript already does this out the box without needing to add as const to it. I'm not sure if maybe older typscript versions didn't do that. I tried it mutating both const array and const object litterals and it all gives red warning underlines for it out the box with my tsx files if you try to mutate them

    • @LachlanMiller
      @LachlanMiller  4 месяца назад

      @@malcolmvernon6808 I think older versions of TS did not do this, glad it's getting stricter

  • @EdwinMartin
    @EdwinMartin 9 месяцев назад +2

    “It isn’t a true constant variable, like it is in other languages”
    That not true. In C++, Java (final), C# (sealed) and probably others, it works just like JavaScript.

    • @LachlanMiller
      @LachlanMiller  9 месяцев назад +2

      My bad, I don't know enough other languages. I guess what I meant was I expect keyword claled "const" to be immutable and, well, constant! Thanks for pointing this out.

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

    2:17 - it's tuples

    • @LachlanMiller
      @LachlanMiller  9 месяцев назад +2

      It's called a tuple? I thought a tuple was something different, like `type T = [number, string]`. Is this really the same type?

    • @snatvb
      @snatvb 9 месяцев назад +1

      ​@@LachlanMiller ahhh, maybe I understand you wrong
      when you use "as const" for array you will get tuple (really strict, with literals in your case), as I get you mean foo[number] - this way to get something from foo
      it's part of cycles in typescript (keyof works same inside ts)
      you can use foo[string] as well, or template strings inside recursive types to filter something