3 ms·
This shouldn't happen... unless someone finds a way to do this without a runtime overhead, which naturally seems impossible. If this is done, TS will become so
by can16358p 3y ago
This shouldn't happen... unless someone finds a way to do this without a runtime overhead, which naturally seems impossible.
If this is done, TS will become something entirely different as it currently doesn't have a runtime: it's a huge-and-smart linter.
But that's all about it. No matter how huge, it's a linter.
Reflection would imply something beyond that. Even though I absolutely love reflection, I believe it should not be integrated into TS.
- vbezhenar 3y agoIt has nothing to do with any runtime. You just write const personType: Type = typeof(Person); This code gets compiled into const personType = { name: "Person", fields: [ { name: "lastName", type: "string" }, { name: "firstName", type: "string" }, ], } There's no runtime. It's just an object which describes layout of some time. If you would implement validation by hand, you'd come up with absolutely conceptually identical code. Look at any validation libraries out there. The only difference is: you specify types with nice TypeScript language, not with some made-up abomination DSL.
- can16358p 3y agoOh okay I was thinking of more of a runtime-code-emitting-dynamically kind of thing. If it's the way you've shown it'd be great!
- mrkeen 3y agoNo runtime necessary, if I understand the problem. Input is bytes. Bytes will never carry their type information (and if they did, you couldn't trust them). So the type information needs to be used in the Deserialiser, not its input. To get typed deserialisation of Foo, the compiler needs to generate a FooDeserialiser automatically for you. The compiler can't depend directly on Foo (Foo is written after the compiler), but if Foo could somehow emit its type information during compilation, then the compiler could depend on that.
- can16358p 3y agoThen I'm all in. I thought something done at runtime dynamically, instead of statically genereting type information at compile time. It'd be lovely the way you described.