Live data from Hacker News

TypeScript please give us reflection/runtime types

github.com

11–20 of 287 posts

Re: TypeScript please give us reflection/runtime types

#11
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…

typescript enums exist and are compiled to javascript (other than const enums)

Re: TypeScript please give us reflection/runtime types

#12

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.

this wouldn't work for interfaces, or more complicated types

Re: TypeScript please give us reflection/runtime types

#13
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…

Dynamically typed means I can look up the type of an object at runtime. JavaScript lets us do this with classes/prototypes. Since TypeScript adds types and enums and interfaces, why can’t it let us look up those at runtime too? It would seem to fit with the way JS works.

Re: TypeScript please give us reflection/runtime types

#14
I vaguely remember one of typescripts developers stating that if they had to start it all over again, enums would not be added; as enums are the only thing emitting runtime code.

Typescript does not change runtime behaviour, there is no special typescript {#if}

Re: TypeScript please give us reflection/runtime types

#17
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…

For any sufficiently large project, dynamically typed languages are not nice, they are a series of interweaved disasters consisting of constantly tripping over what you thought was your feet but is actually NaN and trying to fall forward rather than collapsing entirely.

In every workflow I've seen, TypeScript is already a language that gets compiled to JS. It might as well take full advantage.

Re: TypeScript please give us reflection/runtime types

#18
I’m mainly a backend programmer, so I know just a little typescript, but I don’t really understand what they’re asking for here. It sounds like they want to serialize and deserialize typescript types automatically? If so, that sounds like a good fit for a library, not something most languages offer. (Please educate me if I’m missing the point)

Re: TypeScript please give us reflection/runtime types

#19
Very interesting post to me. There always was the argument that TS should be erasable and carry no runtime information, enums and decorators are a mistake, etc.

For what it's worth, I want my production bundle to be as small as possible. Most people use bundlers among TypeScript, which further complicates things.

I would love a standard library to e.g. generate type predicate functions from TS types.

Taming the beast from this end might even end up useful for things like compilers, no?

TS provides a nice mechanism to be strict in the frontend and validate types using runtime code, that is, type predicates.

But from a higher level, this has never been enough, and you want to map JSON to TS types using some kind of schema definition.

I would love to just be able to pass in a TypeScript type and generate its type predicate at compile time.

Unfortunately, this is impossible due to the halting problem.

So, a standard solution for the case of a plain serializable JS/JSON object and the according TS interface would be very much welcome.

Edit: and all of this is of course about data from external sources (requests, DOM, compiler...). For all of the code and data known at compile time, there is rarely a need to validate types, as they are statically analyzable.

So there is a huge scope that TypeScript has to cater to. It includes JS projects where external type definitions often become obsolete and people who code in TS but do not enable strict mode for their own code (that is, disallow "any"). And all of these type defintions have to be interoperable.

For API requests, this is basically calling for a default generic REST client, and I agree that would be useful. The REST client could use type predicate functions extensively.

Re: TypeScript please give us reflection/runtime types

#20
post #11
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…

typescript enums exist and are compiled to javascript (other than const enums)

And that's one of the features of typescript that really sucks. String union types work so much better.
Post reply on HN