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.
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).
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. 😉
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.
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)
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!
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
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.
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.
you solve typescript types as if you don't know anything else... well done man.
Everyone needs a hobby 😝
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.
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
You never fail to amaze us ! This is wonderful !!!
Thank you so much for your kind words. Glad it is helpful ☺️🤘
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
This channel is just super fun! I’m super curious on how you come with these ideas though.
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 🤘
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).
You could but it could make your types quite recursively deep and not that well readable. So always keep that in mind 🤘👍
Wow typescript is so cool and powerful. Maybe it can tell me if func could throw an error, oh wait.
While I see your point, many languages think checked exceptions were a mistake. Often they just get thrown further.
This is the biggest issue in typescript by far
every function can throw an error, a stack overflow or out of memory, checked exceptions are just irritating
Fantastic content and we'll presented!
Thank you 😁
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. 😉
Wow this is awesome
Subscribed
Awesome, thank you!
what is the extension/plugin/tools that displays the result when using ^?type? this looks very useful
It‘s called „twoslash-queries“ and it also exists for Jetbrains. There it‘s called „witt“
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.
Thank you 🤘, I agree for that purpose it‘s not helpful.
Can you explain how to limit a number to range. Currently if I want to restrict a number to be `
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)
I opening a PR with this type first thing monday morning
Keep me updated if they liked it 🤘😁
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!
Thank you 🤘. It‘s called „twoslash-queries“ for vscode and „witt“ for Jetbrains.
There are no real usage for this
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
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.
@@Typed-Rocksthis gives compile time support also. String.length is runtime
you should work with valibot
I will look into it. Looks interesting 👍🤘
Counter doesnt need to be an array. It should just be a readonly number
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.
Ah yes i just tried and i cant just increment numeric type. Sorry for saying that without confirming it. You are awesome
@ no worries. Thanks for your comment 😁
it would be great to have possibility to add own error messages :)
We could make it so that instead of never, it returns an object type with an error property in
@@Typed-Rocks but it's not an really error. I mean improvement TS compiler :) but it's just kinda my "ny wish" :D
@@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)
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.
The typechecker is quite good in caching this stuff. But of course only use it if you have a usecase 👍