TypeScript please give us reflection/runtime types
1–10 of 287 posts
Re: TypeScript please give us reflection/runtime types
#2One needs to define their types as a const, like this one:
``` [{ type: Number, name: "field1" }, { type: String, name: "field2" }] as const ```
and then use Typescript magic to convert it into the fully fledged Typescript type.
Yes, it's annoying, but it's more flexible.
Re: TypeScript please give us reflection/runtime types
#3AFAIK the best current solution is emitlDecoratorMetadata. [1]
[1] https://www.typescriptlang.org/tsconfig#emitDecoratorMetadat...
Re: TypeScript please give us reflection/runtime types
#4Re: TypeScript please give us reflection/runtime types
#5There are many languages that compile to JavaScript. Pick one of them and use it!
And I have the feeling, that people who want runtime typed Typescript would rather like to write Java/OOP style code instead of JavaScript. But JavaScript is a dynamically typed language, and that's also nice. Be happy with what you have!
Re: TypeScript please give us reflection/runtime types
#6Re: TypeScript please give us reflection/runtime types
#7TypeScript is an entirely optional layer on top of JavaScript, with the exception of `Enum` that emits an object, many of which see as a mistake. You don't even have to "transform" TS code to get JS: you just have to delete the typings. The rest is just JS.
Unless you want to depart heavily from this principle, which I think is crucially important so long as there's still human-maintained JS code out there, what I think they're asking for is a library to take the types and write serializers/validators for. Which there are many of. So I think the real request is to make the one canonical choice out of a field of many choices, which I also agree with the spirit of. But is that really in scope for the TypeScript project?
What I personally don't want is runtime reflection for TypeScript. Or at least, not a core part of the language. Because I like that TypeScript is just for compilation and at runtime you just have JavaScript, without any sort of abstraction layer that turns JS into some sort of intermediate representation with overhead or more indirection for debugging. I cherish the fact that even without source mappings, my output JS is perfectly legible and debuggable.
Re: TypeScript please give us reflection/runtime types
#8Typescript already kind of has it, it's just they got it backwards. One needs to define their types as a const, like this one: ``` [{ type: Number, name: "field1" }, { type: String, name: "field2" }] as const ``` and then use Typescript magic to convert it into the fully fledged Typescript type. Yes, it's annoying, but it's more flexible.
Re: TypeScript please give us reflection/runtime types
#9There is a good reason for not doing this. Typescript would become some kind of runtime on top of JavaScript. A new language that compiles to JavaScript. Currently TS is only JavaScript with type annotations. There are many languages that compile to JavaScript. Pick one of them and use it! And I have the feeling, that people who want runtime typed Typescript would rather like to write Java/OOP style code instead of J…