5 ms·
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 frustrat
by chucklenorris 4y ago
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
- janosdebugs 4y agoHow 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.
- chucklenorris 4y agoSomething like typeinfo<T> being inlined into an object describing my type. Totally opt-in, no need for every type to be tracked at runtime.
- samtho 4y agoDoes 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.
- consilient 4y agoNo, 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. https://www.typescriptlang.org/tsconfig#target https://www.typescriptlang.org/tsconfig#target
- janosdebugs 4y agoTry running tsc in ES2020 mode, the output will be surprisingly clean JavaScript. Older versions include a lot of hacks for dynamic module loading, etc.
- esrch 4y agoYou might be aware of it already, but I just wanted to mention that the Zod library could potentially solve some of your needs.
- chucklenorris 4y agoYes, 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.
- Gigachad 4y agoWhy 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.
- DangitBobby 4y agoYou are sometimes interacting with external data. Particularly in web land, you don't know for sure if the payload meets expectations until you check.
- pharmakom 4y agoPlenty 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.
- DangitBobby 4y agoSure. 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.
- draw_down 4y ago[dead]