This WILL CHANGE How You Work With Strings - Advanced TypeScript Magic!

Поділитися
Вставка
  • Опубліковано 20 січ 2025

КОМЕНТАРІ • 47

  • @MrAlao675
    @MrAlao675 19 днів тому +18

    you solve typescript types as if you don't know anything else... well done man.

    • @Typed-Rocks
      @Typed-Rocks  19 днів тому +6

      Everyone needs a hobby 😝

  • @LePhenixGD
    @LePhenixGD 19 днів тому +6

    You never fail to amaze us ! This is wonderful !!!

    • @Typed-Rocks
      @Typed-Rocks  19 днів тому +1

      Thank you so much for your kind words. Glad it is helpful ☺️🤘

  • @punithg1572
    @punithg1572 17 днів тому +2

    This channel is just super fun! I’m super curious on how you come with these ideas though.

    • @Typed-Rocks
      @Typed-Rocks  16 днів тому +1

      Thank you 😁, I like to fiddle around with the more unusual parts of languages so as soon as you start to do that, the ideas will come naturally 🤘

  • @jfftck
    @jfftck 16 днів тому +12

    Isn’t this getting outside the domain of types? I don’t understand why you would really want or need this, it is a lot of computation on something that should be a concrete type definition. This is bad practice for typing and I wouldn’t use this code in my code base, it’s also pointed out that runtime checks are necessary for any values that are not a constant.

    • @HelloWorld-rx1iy
      @HelloWorld-rx1iy 12 днів тому

      yep, same! most of the stuff i (or we) deal with are dynamic data (eg: pulling out data from db / api(s)), so you can't leverage this in your projects

  • @KamelJabber1
    @KamelJabber1 16 днів тому +1

    Fantastic content and we'll presented!

  • @pierwszywolnynick
    @pierwszywolnynick 17 днів тому +5

    very cool but as always - please don't actually use it, especially for comparing large numbers; you will kill the compiler and IDE performance quickly

  • @danko95bgd
    @danko95bgd 19 днів тому +5

    Wow typescript is so cool and powerful. Maybe it can tell me if func could throw an error, oh wait.

    • @Typed-Rocks
      @Typed-Rocks  18 днів тому

      While I see your point, many languages think checked exceptions were a mistake. Often they just get thrown further.

    • @gerkim62
      @gerkim62 18 днів тому

      This is the biggest issue in typescript by far

    • @pierwszywolnynick
      @pierwszywolnynick 17 днів тому

      every function can throw an error, a stack overflow or out of memory, checked exceptions are just irritating

  • @BeeBeeEight
    @BeeBeeEight 19 днів тому +1

    Amazing work there. I remember u covered how to make sure rgb values has exactly 3 numbers with template literals. Now I feel like trying with this method to make sure those numbers are within range (0-255).

    • @Typed-Rocks
      @Typed-Rocks  19 днів тому +1

      You could but it could make your types quite recursively deep and not that well readable. So always keep that in mind 🤘👍

  • @precisionchoker
    @precisionchoker 19 днів тому +2

    Wow this is awesome
    Subscribed

  • @balaclava351
    @balaclava351 18 днів тому +2

    Can you explain how to limit a number to range. Currently if I want to restrict a number to be `

    • @HelloWorld-rx1iy
      @HelloWorld-rx1iy 12 днів тому

      you could use tuple types since they have fixed lengths, but i would be against stuff like this in real codebases since its gonna cost a lot of performance (and may error out for excessively deep instantiations)

  • @benhaynes8869
    @benhaynes8869 13 днів тому

    what is the extension/plugin/tools that displays the result when using ^?type? this looks very useful

    • @Typed-Rocks
      @Typed-Rocks  13 днів тому +1

      It‘s called „twoslash-queries“ and it also exists for Jetbrains. There it‘s called „witt“

  • @mpcref
    @mpcref 14 днів тому

    A nice use case for this would be defining JSON schema's which can be used to check JSON documents at compile-time using import. Here's a nice challenge for you: implement types for url's and emails. 😉

  • @djs-vids
    @djs-vids 17 днів тому

    hihi! How do you view the result of the type as a comment like that (3:04)? You were using a different IDE before but not it's finally VSCode I think. Great content btw!

    • @Typed-Rocks
      @Typed-Rocks  17 днів тому +2

      Thank you 🤘. It‘s called „twoslash-queries“ for vscode and „witt“ for Jetbrains.

  • @dave6012
    @dave6012 19 днів тому +2

    I opening a PR with this type first thing monday morning

    • @Typed-Rocks
      @Typed-Rocks  19 днів тому

      Keep me updated if they liked it 🤘😁

  • @aram5642
    @aram5642 15 днів тому

    I don't think it will change the way I work with strings, because most of the strings I work with come from api and are not statically analyzable. But it is definitely a great lesson.

    • @Typed-Rocks
      @Typed-Rocks  14 днів тому

      Thank you 🤘, I agree for that purpose it‘s not helpful.

  • @ИльяСтрелец-ц4к
    @ИльяСтрелец-ц4к 18 днів тому

    Think it's better to take string's "length" property directly, as it is readonly despite arrays length property which can be set directly by programmer

    • @Typed-Rocks
      @Typed-Rocks  18 днів тому +2

      In types you can‘t use the length property of the string as it‘s just „string“. That‘s why you need to do that trick.

    • @kettenbach
      @kettenbach 16 днів тому

      @@Typed-Rocksthis gives compile time support also. String.length is runtime

  • @kuyugama
    @kuyugama 16 днів тому +2

    There are no real usage for this

  • @moneyfr
    @moneyfr 19 днів тому +3

    you should work with valibot

    • @Typed-Rocks
      @Typed-Rocks  19 днів тому

      I will look into it. Looks interesting 👍🤘

  • @AbhiShake-pl3cf
    @AbhiShake-pl3cf 18 днів тому

    Counter doesnt need to be an array. It should just be a readonly number

    • @Typed-Rocks
      @Typed-Rocks  18 днів тому

      It cant, as we need to update the counter every recursive iteration. Otherwise we would need to create an array and increment it and put it back into a number each time.

    • @AbhiShake-pl3cf
      @AbhiShake-pl3cf 18 днів тому

      Ah yes i just tried and i cant just increment numeric type. Sorry for saying that without confirming it. You are awesome

    • @Typed-Rocks
      @Typed-Rocks  18 днів тому

      @ no worries. Thanks for your comment 😁

  • @mjerez6029
    @mjerez6029 14 днів тому

    This is neat trick but wouldn't use it, pretty sure Typescript checker will degrade pretty quick if code start to be crowded with there kind of tricks. Impressive none the less.

    • @Typed-Rocks
      @Typed-Rocks  14 днів тому

      The typechecker is quite good in caching this stuff. But of course only use it if you have a usecase 👍

  • @snatvb
    @snatvb 19 днів тому

    it would be great to have possibility to add own error messages :)

    • @Typed-Rocks
      @Typed-Rocks  18 днів тому

      We could make it so that instead of never, it returns an object type with an error property in

    • @snatvb
      @snatvb 18 днів тому

      @@Typed-Rocks but it's not an really error. I mean improvement TS compiler :) but it's just kinda my "ny wish" :D

    • @HelloWorld-rx1iy
      @HelloWorld-rx1iy 12 днів тому

      @@snatvb you might as well make an eslint rule for that lol (assuming smth like this doesnt exist, i am not well versed into the rules)