The `satisfies` operator in TypeScript is a game changer

Поділитися
Вставка
  • Опубліковано 31 лип 2024
  • Learn more about `satisfies` in TypeScript 4.9 www.typescriptlang.org/docs/h...

КОМЕНТАРІ • 46

  • @babelette
    @babelette Рік тому +30

    I wish you made a whole js course in mini shorts format.

  • @Steve8708
    @Steve8708  Рік тому +2

    Read about some killer use cases for `satisfies` in my full article: www.builder.io/blog/satisfies-operator

  • @confuze1500
    @confuze1500 Рік тому +8

    Lmfao I was struggling with this for days and now I randomly found the sollution while watching shorts

  • @omomer3506
    @omomer3506 Рік тому +21

    Nuff said 👏

  • @MarkJaquith
    @MarkJaquith Рік тому +12

    Dang, this is a good example.

  • @MazanLabeeb
    @MazanLabeeb Рік тому +14

    make more videos on typescript

  • @ufukbakan4741
    @ufukbakan4741 Рік тому +1

    Same applies for generic T extends Color vs. Color

  • @ryanbattista343
    @ryanbattista343 Рік тому +2

    These videos are soo good

  • @engine_man
    @engine_man Рік тому

    Love your videos

  • @ChillAutos
    @ChillAutos Рік тому +1

    So satisfies checks to see what the actual type is and then does some type checking in the background to see what it could match on that type and narrows it for you? If so that is insanely cool.

  • @fParad0x_
    @fParad0x_ Рік тому

    Very nice 👍

  • @Seedzification
    @Seedzification Рік тому +3

    Ok but remove satisfies on bleu and you get the same result🙄

  • @kbtganesh
    @kbtganesh Рік тому +1

    Why not just `const blue:Color`

  • @voidbyte
    @voidbyte Рік тому +1

    Where do u learn TS, other than courses. I prefer reading online.

  • @blueghost512
    @blueghost512 Рік тому +1

    It is a game changer, but how can we use it in function params?

  • @developer_hadi
    @developer_hadi Рік тому +2

    Why would I set types for a constant variable?

    • @Steve8708
      @Steve8708  Рік тому +4

      To make sure they confirm to the necessary shape when authoring (including things like autocomplete in your IDE), as well as better refactoring etc

    • @arakwar
      @arakwar Рік тому +1

      Autocompletion.

  • @ThomasGHenry
    @ThomasGHenry Рік тому

    Feels like footgun.

  • @howuseehim
    @howuseehim Рік тому

    Can you teach us how you do your subtitles?

  • @joaofrancisco5781
    @joaofrancisco5781 Рік тому

    I always uses the first method

    • @ko-Daegu
      @ko-Daegu Рік тому

      Means you never wrote tsx files before?

  • @vegardertilbake1
    @vegardertilbake1 Рік тому

    Wouldn't casting as one of the type from the union type work the same way?

    • @NN-kf7qo
      @NN-kf7qo Рік тому

      I guess it's just more explicit

  • @rutpshah
    @rutpshah Рік тому +5

    Usecase of satisfies?

    • @Steve8708
      @Steve8708  Рік тому +2

      Configuration ua-cam.com/video/xPhGANCF6Pc/v-deo.html

    • @chintandesai2236
      @chintandesai2236 Рік тому +1

      ​@@Steve8708 you earned a subscriber

    • @arakwar
      @arakwar Рік тому

      Declaring a constant that follows a type but still get type inference. This way, any « typeof » you do on your constant gets the infered type, not the « base » one.
      This then can be used in mapped types to create new types based on your constant.
      I’m using this to create a constant where I map attributes from my API to my app data, then use mapped type to create the types for that data.
      This way, anything I add in my mapping contant is also added on my types and I can start using them right away.

    • @QwDragon
      @QwDragon Рік тому

      There're lots. Wery useful when object will be passed to generic and transformed somehow depending on actual shape.

  • @vikingthedude
    @vikingthedude Рік тому

    So would it be correct to say that the first two are type annotations but the third one is a type assertion?

  • @ko-Daegu
    @ko-Daegu Рік тому

    JS word feels like DLC
    They give you a feature then another one that does the whole work for y’a

  • @theLowestPointInMyLife
    @theLowestPointInMyLife Рік тому +1

    That's bad code

  • @JeyPeyy
    @JeyPeyy Рік тому +1

    Never do #2 if possible

    • @d.ilnicki
      @d.ilnicki Рік тому +1

      Never give any advices if you want to provide no explanation.

    • @JeyPeyy
      @JeyPeyy Рік тому

      ​@@d.ilnicki You lose type information without adding any safety or other benefit.

    • @QwDragon
      @QwDragon Рік тому

      @@d.ilnicki Second allows more flexible casts.
      const x = true as false; // Compiles without error
      const y: false = true; // Type 'true' is not assignable to type 'false'.

  • @44r0n-9
    @44r0n-9 Рік тому

    kinda obvious

  • @trplnr
    @trplnr Рік тому

    So satisfies literally checks if it satisfies the type

    • @MM-24
      @MM-24 Рік тому

      That cant be right, they all satisfy the type, and each way would ensure that check...it appears that satisfies does not mask the actual type.
      The other methods override ...somehow

  • @Umer-
    @Umer- 2 місяці тому

    Typescript has become a joke with these sematics.

  • @oneinazillion
    @oneinazillion Рік тому

    if this is backend code, i'd rather not play russian roulette with types

  • @wissemaljazairi
    @wissemaljazairi Рік тому +1

    Why do they invent useless language features like this?

    • @NathanHedglin
      @NathanHedglin Рік тому +1

      If you think they're useless then you're a bad developer. Maybe learn something yeah?

  • @sp3cterproductions
    @sp3cterproductions Рік тому +2

    strict. typing. avoid all of this.

    • @QwDragon
      @QwDragon Рік тому +2

      First and third are absolutely strict.