Re:All I Want for Christmas Is These Seven TypeScript Improv...
by chucklenorris on Sunday December 25, 2022 @11:02PM
(#34134443)
I want actual reflection a la python so i can use my types to generate all sorts of artifacts for runtime validation, serialization, testing, etc. It's frustrating to make the effort to create the types and then not be able to touch them at runtime
[ Parent
| on HN ]
Re:All I Want for Christmas Is These Seven TypeScript Improv...
by esrch on Sunday December 25, 2022 @11:31PM
(#34134608)
You might be aware of it already, but I just wanted to mention that the Zod library could potentially solve some of your needs.
Re:All I Want for Christmas Is These Seven TypeScript Improv...
by chucklenorris on Monday December 26, 2022 @04:07PM
(#34141890)
Yes, that and the openapi plugin is what i use right now. Before i used to load the ts compiler at runtime and tell it to parse my file and got the types from there. But it was a stupid overengineered brittle solution.
Re:All I Want for Christmas Is These Seven TypeScript Improv...
by janosdebugs on Sunday December 25, 2022 @11:28PM
(#34134585)
How would Typescript do that while transpiling to JS? It would need to add a whole lot of boilerplate code to the generated JS, slowing the execution down.
Re:All I Want for Christmas Is These Seven TypeScript Improv...
by samtho on Monday December 26, 2022 @01:13AM
(#34135143)
Does it not already do this? I don’t use TS if I have the choice due to all the runtime complexities, including the endless house-of-cards shimming it does to prop up all the features of TypeScript.
Re:All I Want for Christmas Is These Seven TypeScript Improv...
by consilient on Monday December 26, 2022 @01:25AM
(#34135230)
No, typescript has full type erasure and almost no type-directed emit. The shimming you're seeing is probably the result of tsc downleveling newer javascript features to older browsers: typescript itself has nothing to do with it.
Re:All I Want for Christmas Is These Seven TypeScript Improv...
by janosdebugs on Monday December 26, 2022 @03:28AM
(#34135804)
Try running tsc in ES2020 mode, the output will be surprisingly clean JavaScript. Older versions include a lot of hacks for dynamic module loading, etc.
Re:All I Want for Christmas Is These Seven TypeScript Improv...
by chucklenorris on Monday December 26, 2022 @04:02PM
(#34141836)
Something like typeinfo<T> being inlined into an object describing my type. Totally opt-in, no need for every type to be tracked at runtime.
Re:All I Want for Christmas Is These Seven TypeScript Improv...
by Gigachad on Sunday December 25, 2022 @11:42PM
(#34134665)
Why should types even exist at runtime? Everything should be validated at compile time and then the whole concept of types no longer needs to exist.
Re:All I Want for Christmas Is These Seven TypeScript Improv...
by DangitBobby on Monday December 26, 2022 @02:57AM
(#34135678)
You are sometimes interacting with external data. Particularly in web land, you don't know for sure if the payload meets expectations until you check.
Re:All I Want for Christmas Is These Seven TypeScript Improv...
by pharmakom on Monday December 26, 2022 @05:20AM
(#34136384)
Plenty of solutions here such as code generation or Elm style combinators. I’m not convinced that Python style runtime meta programming is a particular robust solution since it is not (easily) checked statically.
Re:All I Want for Christmas Is These Seven TypeScript Improv...
by DangitBobby on Monday December 26, 2022 @05:41PM
(#34142719)
Sure. But it would be nice not to need to describe the structure of your data twice if you need runtime validation. Since TS is such an ergonomic way of describing the structure of your types, runtime availablility is more attractive (to some) than relying on external libraries for that functionality.