Monteluna pfp
Monteluna
@monteluna
@darrylyeo GADTs in Typescript in action. Not really the best example because I'm not using recursive, but a good example. It slightly increases the codebase, but it moves business logic into the type system. By using them with the case statements below, it forces the developer to be explicit about the state of the code, and handle each possible state. https://github.com/enviodev/velodrome-indexer/pull/287/files#diff-b5265cb45df3c9b118cf42de25e3f34b3b838c88120cd40d41c36ca2c9293d29R177-R214
1 reply
1 recast
3 reactions

Darryl Yeo đŸ› ïž pfp
Darryl Yeo đŸ› ïž
@darrylyeo
Love it! GADTs are absolutely the way to go and will only get easier to use now that TypeScript 5.8 can narrow them directly off of JavaScript control flow. https://warpcast.com/darrylyeo/0xde3a8327
1 reply
0 recast
0 reaction

Monteluna pfp
Monteluna
@monteluna
So there are a few things that are annoying. There really aren't implicit type guards that force you to handle all the types. In the switch statement, you have to add that weird default case with a "never" type, and inhabit that type in that codeblock. The point is if you create a new type, and the case statement can reach that default case, the codeblock will error at compile. What I really want is a method dispatch to do this at the function definition level and not with weird switch/case statements.
1 reply
0 recast
1 reaction

Darryl Yeo đŸ› ïž pfp
Darryl Yeo đŸ› ïž
@darrylyeo
Yeah, dead control flow branches could still be much smarter. Considering they’re still actively updating the type narrowing engine I think all of these annoyances will be solved eventually.
1 reply
0 recast
0 reaction

Monteluna pfp
Monteluna
@monteluna
I guess it's also, most JS engineers don't use this stuff. I was dragging this out for Event handling server type work. Your median engineer using typescript only works with Front End code. I haven't seen server side JS in the wild in a long time, where this stuff is definitely useful.
1 reply
0 recast
1 reaction

Darryl Yeo đŸ› ïž pfp
Darryl Yeo đŸ› ïž
@darrylyeo
Oh, I’ve seen the haphazard spaghetti way people check for properties on the frontend side too, haha. Discriminating control flow cases based on a single enum or string property is simply way superior whether you use TypeScript or regular JavaScript.
0 reply
0 recast
1 reaction