Everyone's talking about HOTScript
Вставка
- Опубліковано 13 кві 2023
- HOTScript: github.com/gvergnaud/hotscript
Become a TypeScript Wizard with my free beginners TypeScript Course:
www.totaltypescript.com/tutor...
Follow Matt on Twitter
/ mattpocockuk
Join the Discord:
mattpocock.com/discord
Beautiful in the most scary kind of way. Most TS devs I know barely understand generics. I dont expect to see this in our code base for another 10years.
You mean in production code?
I think these kinds of utility types are mostly for libraries that have to support a variety of consumers
Higher order types could actually be useful in quite regular code. For example, type T = A isn't possible without higher order types and you don't have to be a genius to figure out what it does
@@peterjasko7059 I am far from a genius. What does it do? Or is that the point?
@@GavinKing_AKA_plumpNation : Looks like T is just a buffer of sorts. Not knowing Typescript, I assume that A< B > results in a type being defined, at which point I'm forced to suspect that that the provided example implies that Typescript doesn't support type descriptors as a data type.
I feel like it would be much more desirable to just have proper compile-time meta-programs written in actual TypeScript instead of trying to essentially duplicate the entire language in the type system.
This is a trap that many type systems seem to fall into. Sooner or later they become Turing-complete, because people understandably want to do arbitrarily complex things. Then they start writing meta-programs in the type system, which is just not designed for that kind of work.
You're essentially forcing the compiler to do compile-time code execution, but in the most convoluted way possible. Compilation times will suffer massively (see C++ templates and similar systems).
Me: Looks nice
TS LSP:
TS LSP but 10 minutes later: Agree
TypeScript is reminding me more and more of my thousands of hours painting my 40k miniatures and my paltry couple dozen hours actually playing the game.
Lmao that’s so accurate
It's true! The sort of nice thing about that is that you are painfully aware that type-space work is intrinsically 'useless', instead of thinking your doing something useful by following big boilerplatey OOP patterns - instead there is this big line between inert typing code and active js code
I think freewheeling is totally fine as long as you're showing something valuable. In this case you introduced me to a new pattern in TypeScript. I'd love to see you analyze more bleeding edge TypeScript libraries and just talk about new/emerging/interesting patterns being used
Great format Matt, hoping to see more!
This is a great video for me a senior typescript dev looking to stay in the know. Keep them comming :)
Definitely here for this style of video as well!!
As a functional programming enthusiast since 2012..I dig this so far. Also, I like this style of video too. Good on ya mate.
To quote my favorite show my kids watched, “But why?”
I really liked this recommendation and how you presented it, no need for heavy editing! I will think about how I could use this in one of my projects.
I like this type of video, cause it tells me about cool things I could look for and use in the future without me having to go around the internet discovering everything myself. I really like your videos. I think I never really knew much about typescript as much as I do now. I don't think I'm a wizard tbh, I'm more like a casual observer who uses this stuff. Many more people better than me. But I do like listening and learning all the same. Great job!
This
Yeah Fireship but for typescript 😌
Yes, like this style of video. always great to stay up to date
Oh, I've GOT to use this in my lib. I knew this kind of thing was possible, I just wrote a type that creates recursive types with multiple recursion points at any level of nesting (to get around the limitations of, e.g., Zod Lazy) but this Library will save me hours and hours of creating complex type incantations. Maybe I'll have a few of my own to contribute!
More like this! Great work, Matt! 👍👍👍
I used this for code-golf a while back and was very impressed by how useful it is. It turns TS into a functional programming language.
yes, please more of those videos. "the community is talking about it but you've been busy, let's sum it up in 3 minutes" is the perfect angle for me.
I like this style of video, and the content. Keep up the good work :)
This looks like one of those things that I stare at for ages thinking "yeah... but what the hell is it FOR?" until some "wizard" comes along and say "oh, it's for THIS" and shows me something amazing. ;)
Thanks, Matt. I'm going to check this out, too.
I like these videos!!
Jeep sharing your discoveries here
I like the format.
Typescript really is amazing.
Not sure this kind of “meta programming” is applicable for most people, but it still really cool to see and understand how this stuff works.
There is a big difference between what can be expressed in typescript and what should be expressed in typescript
This is crazy and goooooooood! I'm tired of mapping every type manualy.
Sounds like a great pick for library authors
I also "found" this library two days ago when I researched for efficient type level arithmetics in typescript. The last few weeks I have been on a journey deep into the depths of typescript's type system and I gotta admit, it is pretty awesome. This way of inferring properties defined by later intersections however feels like wizardry
I was doing the same thing-type-level arithmetic.
Its very addictive. You could do this before just with generic types, but this library makes the process a lot more beginner-friendly
Interesting. I've always sort of considered things like "Pick" and "Omit" and ReturnValue" to be sort of like type-level-functions. This seems to be taking that to the logical conclusion.
Wonder though if it's a solution in want of a problem? The examples seem a bit contrived, especially the one where they just do a bunch of logic on types that can only contain a single value. (i.e. the 95 one).
I was thinking the same thing. I’m struggling to see how this abstraction makes any difference in the real world. I also imagine types that are heavily nested using these primitives will lead to the TS chose of infinitely nested death.
I felt the same way, the example shown in the video seemed really contrived, but it looked really intriguing and haskell-y. On the repo itself there is a much better example (the one with an accompanying video demo) where it's used to infer typed params from a URL, which seemed more practical to me. I don't know enough about typescript or use it in a way that would need that, but I could see it being useful to at least some library authors. Maybe even a lot of libraries!
@@Quiqsave I mean I *did* have the idea that this could be used to implement a Monad library! monads are supposed to be higher order generics, i.e. something like Monad where M can take on the value of (for example) 'Promise'. But not 'Promise', just 'Promise' -- which you can't do in typescript because that's not a valid type (you can't do in most languages).
Or at least I thought you couldn't do it in typescript. But this approach seems to blur the line between a fully instantiated type that can be used as a generic argument, and the generic itself.
Which is very haskell-y. And very cute.
Practical? Not so sure, but I suppose time will tell. My usual recommendation concerning Monads is to, whenever they come up in a conversation, just slowly back away :)
Oh god, this is metaprogramming with C++ templates all over again!
Looks like a debugging nightmare. LOL.
I wonder how this would effect the performance of type checking if heavily used in a project. I already feel like type checking has mediocre performance in large projects, and can get horrendously slow if devs don't know what they're doing.
There is no type checking in the runtime. The compile times may go up, but come on, who cares much about that?
I assume he is referring to how feedback for LSP gets worse. In essence you IDE gets “slower”
@@kasparpoland9803 Yes, I was referring to performance at compile time. By default, the type checker runs type checking on every keystroke. Even if this only takes 1-2 seconds, it can be very frustrating. I was just working on a TS project that took between 10-15 seconds for types to update *on every keystroke*. I found two types with a circular reference to each other (which is allowed, but a bad idea). After removing the circular reference, the time it took to run type checking dropped to under 500ms.
There may not be performance implications at runtime, but slow type checking can make for terrible DX and create a tangible slowdown of development.
"Your scientists were so preoccupied with whether or not they could, they didn't bother to think if they should..."
I've been looking for a simple way to add HKT to Typescript like PureScript but most of the implementation provide not efficient enough as it always ended up that I needed to dig into the details. Hope that this is different. Thanks for sharing! And good work👍
is it a good idea to overide native javascipt api signature on global.d.ts with this librery to have a more accurate/restrictive type checking
Looks cool, but I really hope they simplify the syntax more. Most of us even struggle with the most fundamental types of Typescript.
Most of us?
I would love it for some of this in my codebase. But I know some that would probably put it on everything, and then some others will not know what to do during a feature request, because TS is giving all sort of error messages.
Seems cool, but… where can we use it
Great video Matt! TS just keep giving me Imposter syndrome. I feel like I have to learn these tricks/obscure things to be able to say I know TS
Wondering if you would like the 'fp-ts' library, it is basically this but for javascript
Can you bring a good useful use case?
Wow this is really cool. Defo would be interested to see some real world applications but it's honestly already very neat just on its own terms
That’s the amazing thing about typescript.
For most people, “vanilla” typescript with a small bit of wizardry is all you need.
But then there is this other layer of typescript magic that is just freaking cool, irrespective of if it is actually useful (currently) in, say, building react front ends
The pipe operator is very similar to rxjs without the async aspect. I wonder if this pattern could improve the type chaining in that lib, or if they are already doing it.
What I thought as well, looks like synchronous rxjs...it would be awesome if that was added to that library
cool vid!
Short videos like this are good, but I was waiting for you to compare writing the same type with and without the library. Even the library fails at the confirmation by not including `const a: res1 = 95` in the playground example, validating it in our brains.
not true. The comment under the variable shows the type is 95
Thanks
Could you give some sort of practical application for this? I don't understand why you would want your typesystem to mutate numbers in a tuple for example.
I mean, it looks intriguing, but I can't imagine what use I'd have for it.
How does one step through debug this code? Newb question.
I know a guy who created a Rust program that solves the 8 queens problem entirely at compile time using the type system. It'd be very interesting to see if libraries like HOTscript also make this possible in TS!
I saw a blog post where someone did it in typescript as well a few weeks ago
Please continue bringing us your revelations
What is the extension that tells you the type with the comment // ^?
ua-cam.com/video/u0adKDu--cA/v-deo.html
I don't think I'll be able to use this at work. My collegues struggle with basic types. They can with some help use my custom generics. But if I throw something like this into the codebase it'll be a struggle.
How is the performance of this? for my own projects I usually run trpc, and that's known to wreck ts-server.
There's an infer in that higher order type, so my guess is, it's not too great if you overuse it.
But definitely fascinating to say the least.
Same here, my colleagues hardly understands the necessity of unit test and even less the needs of using types. But, you'll find it absolute necessary if you roll your own project, to speedup by replace most of the test using types, and scale in complexity
feels like a new debugging nightmare
So first you add typing to javascript to make it more readable, and then you take those types and make them into some weird object-pointers you pass around? I think I now understand what biologists mean when they say evolution is blind.
I think.... The first example is doing something I've seen happen (kinda) in c++ I have no c++ experience at all but I've seen people string standard library functions together to perform complex operations. Either way it's giving Haskell vibes and in the case of tuples it seems very much like J K or APL
Yeah this reminded me of some wizardry you can achieve with C++ templates and constexpr evaluation
Hi Matt, I have a (maybe?) challenge for you. Trying to write a higher order React component whose resulting function takes a generic and forwards it to the passed component IF the component passed to the hoc takes one.
const Result = withHoc(Comp);
...
// no generic if not required by Comp
// generic if required by Comp
This is necessary when Comp's prop takes a callback that needs to pass Type back up to the parent rendering the Result.
Have seen some janky examples, such as where Result is typecast to override the HOCs returned type, but it's never very clean. Maybe you can come up with a more semantic solution with some kind of "generic generic"
I didn't know that I could pass an input to an interface
nuts indeed
And here I am still trying to create a Type to perform bitwise operations on static numbers. I don't think TS is powerful enough yet to be able to do this.
I'm sure if you asked in my Discord we could figure it out.
mattpocock.com/discord
How does 'this extends" work in this case?
this extends { infer Whatever } is a conditional type! And it's testing on the value of 'this', which is only available in interfaces. Pretty wild.
@@mattpocockuk thanks, makes sense!
jesus, as soon as I saw it, it's like, why didn't I think of that?! 🤦 That's awesome! 😆
TypeScript has some pretty good built in ones like `Awaited`, but it's pretty odd there aren't more utility type libraries. Seems so obvious.
I like the freewheeling style better.
This means typescript has more responsibility rather than helping the developer understand their code. I hope it doesn't cause much confusion to new devs thinking what is typescript & it's role in the project. Personally, this looks really simple.
this feels like we finally went full round to "compiled language" ...this is "compilation step" where value is created during "typescript => js compilation" and baked in, costing some computation on developer PC but every client just uses it. We reinvented "static values" again?
It's cool and all, but the question is how often are you gonna use this out in the wild? Probably never.
I think my comment did not make it through. I implemented a version of this called free-types supporting type constraints, and it changes a lot of things. I encourage you to check it out. It has a a good readme and guide explaining how it works and the challenge of making it type safe.
More freewheeling videos!
Whoever created this library should have gotten mail from Hogwarts
Doesn't it drain the battery just to calculate that monstrosity?
Cool.
It sounds a cool library for functional programming
But I believe RxJs may able to do the similar things or even more
(Since I am a bit familiar with reactive way)
May I know any recommendations when to use hot script and when to use rxjs? 🙏
They’re not even the same thing. Rxjs is a runtime thing and Hotscript is at the type level.
Please do a whole video just on the different ways people hack TypeScript to allow for operations on numbers. I have no idea how you accomplish something like that without some crazy set-theoretic definition of a "number"
You should clone HOTscript git. It is actually completely insane how they do power, addition, substraction and multiplication.
Oh man, here we go again with HOC but in ts now. You guys remember how lovely HOC were in react? 😂. Jokes aside, might be not being smart enough or too simplistic but I think this pattern is so hard to read and reason about 😅.
Putting logic in type definitions is probably the most blasphemous thing I've seen, since I switched from JS to TS
It called typedriven development and mathematically proven to be better
There's going to be a time and a place for code like this, for example this will be invaluable for library authors.
@@WayOfTheCode the problem is that ts types don't exist at run time
I cannot even imagine a use-case for this.
I guess one use-case is for example making sure a list contains an exact value? Say you have a yearly budget and you want to make sure the sum is valid... All of which in TS directly?
So you did imagine a use-case then?
@@RD-eh3tz The thing is though, I don't know if this would work in that scenario
Without an example is hard to grasp why it's intriguing or even useful.
I love the idea of (pseudo)arbitrary code execution for the sake of accurate IntelliSense in scripting languages, but the syntax is weird and the performance on this is probably terrible 😬
at this point typescript should just let you write type logic in a subset of javascript.
Why Not fp-ts or effect-ts?both libs are way more powerfull than that..
There's been a talk since forever about Higher Kinded Types in Typescript. This library is unfortunately not that. I can see some utility in this library though, the time will show.
Damn I was thinking of making something like this 😮
Lets make simple types like get method type, just simplyfying underlying typescript
We are playing god.
Why do we need this?
k but why?
Its an interesting library. It feels more clever than useful, but I generally lack imagination :shrug:
You should at least add a goatee or something to brand the more casual videos
Man, F# was really far along all this time.
Why can't you just use functions like pipe and compose to do function composition?
const composition => method => (...fns) => initialValue => fns[method]((value, fn) => fn(value), initialValue)
const compose = composition('reduceRight');
const pipe = composition('reduce');
This is akin to "ramda", but types and not JS operations. You REALLY should look into "ramda".
The people that pioneered HKT techniques get no attention, but the author that wrote an attention grabbing library called "hotscript" gets all the attention. These days, programming is an attention game, you don't have to write something good or new, you only have to be the first to have your stuff mentioned by social media influencers. Why bother to write anything at all if you can be overshadowed by inferior solutions that play the attention game?
I'm still waiting for cuckscript to drop
Bro made the type system turning complete. In all seriousness, I feel like if you need to do this, you have bigger problems in your architecture.
Excluding devs of very type-crazy libraries like Zod or prisma of course.
convert typescript to javascript
Reinventing JavaScript? Lol
Starting to think Rust and WASM might just save frontenders from falling into this Typescript trap. Typescript is nice and I love it, but it’s getting to a point where it’s too clever for your own good.
Ahh, very nice but so unnecessary :D
Oh gosh C++ templates story repeated again
has everyone who writes typescript ever heard of the KISS principle? This is a terrible, awful idea. The entire thing is just moving "business logic" to the type level from the function level, losing both any value gained from debugging tools and further increasing the opacity of code. Performance analysis and optimisation will be terrifying.
i hate this
Typescript is ruining javascript! this is just bonkers