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 please give us reflection/runtime types
11–20 of 287 posts
Re: TypeScript please give us reflection/runtime types
#12Typescript 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
#13There 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…
Re: TypeScript please give us reflection/runtime types
#14Typescript does not change runtime behaviour, there is no special typescript {#if}
Re: TypeScript please give us reflection/runtime types
#15Re: TypeScript please give us reflection/runtime types
#16Re: TypeScript please give us reflection/runtime types
#17There 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…
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
#18Re: TypeScript please give us reflection/runtime types
#19For 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
#20There 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)