Live data from Hacker News

TypeScript please give us reflection/runtime types

github.com

1–10 of 287 posts

Re: TypeScript please give us reflection/runtime types

#2
Typescript 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

#5
There 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 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

#6
I dunno, this seems like it would be out of scope for something like TS proper (there's already been a ton of feature creep, anyway). Reflection is generally implemented via runtimes, which can be very clunky. Is it really worth doing this in JS? Tooling already seems too bulky as is.

Re: TypeScript please give us reflection/runtime types

#7
Above all of what I write below, I think that this is a very valid issue for discussion and debate. I don't think there's one objective correct answer. So this isn't me dictating what TypeScript shalt be.

TypeScript 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

#8

Typescript 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.

U just need a library to enable this to reduce the boilerplate. I don't see how that's impossible

Re: TypeScript please give us reflection/runtime types

#9
post #5

There 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…

You can use Kotlin and compile to JavaScript. It's type system is better and the standard library greater scnr
Post reply on HN