Fix All Issues of Primitive Types: Achieve Perfect Type-Safety with Branded Types in TypeScript

Поділитися
Вставка
  • Опубліковано 15 жов 2024

КОМЕНТАРІ • 40

  • @nathaaaaaa
    @nathaaaaaa 29 днів тому +2

    My criteria on whether I should trust someone's tips or not is by their navigation on their IDE, as this is a sign of not only experience but mastery.
    You are trustworthy.

  • @ryan_t_brown
    @ryan_t_brown 2 місяці тому +5

    I really like your channel, especially how you showcase the problem your solving in a quick and simple way before getting to the solutions.

  • @markyip554
    @markyip554 2 місяці тому +15

    The pattern is known as 'Parse, don't validate'.

    • @parlor3115
      @parlor3115 2 місяці тому +2

      He did validate, though

  • @volvopls
    @volvopls 2 місяці тому

    This reminds me of the book by Scott Wlaschin - Domain Modelling Made Functional. It is for F# but after seeing this video I believe the idea can be applied for TS as well. Really cool stuff!

  • @albert21994
    @albert21994 2 місяці тому +4

    Cool! I’ve learned something today!

    • @Typed-Rocks
      @Typed-Rocks  2 місяці тому +1

      That‘s awesome to hear. Thank you, that‘s what I want to achieve 👍

  • @__azt
    @__azt 2 місяці тому +3

    This is really neat.

  • @bahaaalhalabi8940
    @bahaaalhalabi8940 2 місяці тому +1

    Interesting idea,i will try it out,i usually use asserts but without the branded primitives. Good video 👍

  • @lukewood5751
    @lukewood5751 2 місяці тому +8

    It seems like you could use this to actually make some nicer types for numbers. Something like `Integer` for array accessors, `NotZero` for division, etc. Pretty neat - never have seen someone use these typescript features in this way.

    • @Typed-Rocks
      @Typed-Rocks  2 місяці тому +3

      This is an awesome idea. Will think about making a video about it 🙏

    • @lukewood5751
      @lukewood5751 2 місяці тому

      @@Typed-Rocks I think the challenge would be balancing value vs ergonomics. For example, is it really worth checking every single number you use to index into lists for validity? Maybe - but probably not. It might be worth it if you're doing something that needs to manipulate indices a lot, such as an array-mapped heap. But it does seem a bit rare that this would be worth it. Still, very cool concept that might be worth exploring more.

  • @muchis
    @muchis 2 місяці тому +1

    nice!
    typescript just recently released type validations with regex. Would this change your approach in this video knowing that?

    • @Typed-Rocks
      @Typed-Rocks  2 місяці тому +1

      That‘s a great question. Maybe will also create a video about it. Thank you 🙏. I will look into it

  • @cotyhamilton
    @cotyhamilton 2 місяці тому +1

    Is the // ^? type check thing from an extension?

    • @Typed-Rocks
      @Typed-Rocks  2 місяці тому +4

      Yes, it's an extension I've written called "WiTT" You can find it in the jetbrains marketplace: plugins.jetbrains.com/plugin/23294-witt--typescript-type-hints-like-in-the-ts-playground

  • @_WyreZ
    @_WyreZ 2 місяці тому

    Is it possible to create a dynamic asserts function based on a union type? ie:
    type MyType = {salmon: string} | {bacon: string}
    const myVar: MyType = { bacon: string };
    somehowAssertAsSalmonOrBacon(myVar);
    // myVar (asserted as {bacon: string}, but could be asserted as {salmon: string} if it was initialized that way)?

    • @Perer876
      @Perer876 Місяць тому

      That would not work and not make sense.
      Maybe, what you want is type inference. In your example, you can do this:
      const myVar = { bacon: 'foo' }
      // myVar will be inferred/asserted as { bacon: string } or wherever you initialized.
      Also, you cannot create a dynamic assert function based on a type cuz all type information is erased at compile time.

  • @nomoredarts8918
    @nomoredarts8918 2 місяці тому +1

    It's unfortunate that this is not built-in in TS

  • @thirtykey
    @thirtykey 2 місяці тому

    Wouldn’t typescript then think that it has a __brand property even though it does not, potentially causing a runtime error?

    • @skylerockspecial
      @skylerockspecial 2 місяці тому +1

      No, __brand is a variable holding a symbol. The symbol is the key, not ‘__brand’ itself.
      Important part is the square brackets around the key in the extra object, this sets the value of a variable as the key.

    • @thirtykey
      @thirtykey 2 місяці тому

      @@skylerockspecial yea I know. You know what I mean. That type will be assumed to have that extra field, referenced by that symbol even though it doesn’t.

    • @Kitulous
      @Kitulous 2 місяці тому

      if you don't try to use it on a string while it's coerced to the Email type, no it won't. ​@@thirtykey

  • @coder_one
    @coder_one 2 місяці тому +1

    Branded types are nice, and I am using it a lot, but the problem I have with functions using "asserts" in TypeScript is that such free and randomly throwing of errors inside programs is one of the worst practices and anyone who writes code like this should be ashamed of themselves.

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

      I often use error handlers which handle non recoverable errors. The asserts can also be wrapped in try catch blocks to handle them properly👍

    • @coder_one
      @coder_one 2 місяці тому

      @Typed-Rocks Unfortunately there is no agreement between us here. In my opinion and that of my project team, handling errors with "try-catch" is the worst nightmare. Errors should be treated only as values - this is the safest way to work with errors, because we secure the program and guarantee its stability.

    • @xorlop
      @xorlop Місяць тому

      @@coder_onetry catch turns errors into values… Typed-Rocks is saying they throw and handle it with error handlers. I think Typed-Rocks would agree errors as values is awesome, and that throwing with try catch is effectively errors as values because a throw wrapped in try catch gives you opportunity to turn errors into values. I don’t think there is many people who say try catch is cool or nice. I think we all hate it lol.

  • @wertig6925
    @wertig6925 2 місяці тому +3

    🤘🏻🤘🏻

  • @guicercal
    @guicercal 2 місяці тому +2

    no no, thanks!

  • @joeyfinkel
    @joeyfinkel 2 місяці тому +1

    If the check for the email is only checking if the email contains an “@“, why not just create
    type Email = `${string}@${string}`
    and then move the asserts function into the send email function?

    • @Typed-Rocks
      @Typed-Rocks  2 місяці тому +2

      @@joeyfinkel This check of course was just a demonstration and in reality would be more complex. 😁

    • @joeyfinkel
      @joeyfinkel 2 місяці тому

      ⁠@@Typed-Rockswhat would be a practical example to use a branded type like this?

    • @Typed-Rocks
      @Typed-Rocks  2 місяці тому +3

      @@joeyfinkel a phone number for example which can have different prefixes and lengths

    • @evanbelcher
      @evanbelcher 2 місяці тому +1

      ​@@Typed-Rocksor just an email that actually checks all of the restrictions for an email address, as you implied.

    • @bahaaalhalabi8940
      @bahaaalhalabi8940 2 місяці тому

      He just demonstrated the type-checking, at the end this is just for typescript. In practice you add actual email checking, like for example zod schema for an email inside that function. The includes @ is just a basic example.

  • @gabrielbianchi2246
    @gabrielbianchi2246 2 місяці тому +2

    Looks overcomplicated in my opinion. Just write JavaScript with zod. No need to add so many types and make working with strings more complex in my opinion.

    • @Typed-Rocks
      @Typed-Rocks  2 місяці тому +1

      You can always add zod to the typeguard function. But of course it always depends what you want to achieve. Sometimes you don‘t want to add another library to your project which the team has to get used to. But zod is great nonetheless 👍