deepkit / deepkit-framework

A new full-featured and high-performance TypeScript framework
https://deepkit.io/
MIT License
3.18k stars 121 forks source link

One Typescript Transformer to run them All #475

Closed M-jerez closed 11 months ago

M-jerez commented 12 months ago

Discussion: One Typescript Transformer to run them All

Hello, I been reading a bit after discovering the typescript-needs-types repo and Looks like the TS team will never emit type data to be used at runtime.

There are currently 2 approaches to solve validation which i suppose is the main use case. 1 - Libraries that that declare an schema and then infer a type from that schema. i.e: zod, valibot, etc... 2 - Libraries that use Typescript Transformers to read the AST and either modify the js output or emit type metadata.

As the TS team would never emit types metadata Think we could identify the libraries that use Typescript Transformers and create a single package that suits every current existing library requirements and use that single one instead duplicated efforts. (Just for the part that parses the AST and eemits code or metadata)

I've personally have investigated two libraries that uses typescript transformers, Typia and Deepkit (from what I can see these two are that mayor players using typescript transformers). These two as well have different approaches. Typia emmits custom validation functions while Deepkit emits bytecode representing the type metadata.

Maybe we could create a well documented package that boths emits metadata and use a visitor pattern or similar so can emit custom code when traversing the AST, this would cover both typia and deepkit requirement and would be the base for any other project trying to achieve something similar.

We would nne to chat with @samchom and @marcj and check if they are up to the task. I'm not that expert on using the AST but happy to collaborate on what i can 👍

Note I've opened this same Issue in the involved repos:

marcj commented 11 months ago

Maybe we could create a well documented package that boths emits metadata and use a visitor pattern or similar so can emit custom code when traversing the AST

That won't be possible. AST is too verbose (bundle size) and too slow to interpret (runtime overhead). Hence why Deepkit chose bytecode. Bytecode is flexible and the VM emits as result standardised type representation as simple objects which can be used by other libraries for whatever (validators, serializers, openAPI, ...). See https://github.com/deepkit/deepkit-framework/blob/master/packages/type/src/reflection/type.ts.

So while I think it would be a good idea to standardise something to be used by others well, I don't see much flexibility in how or what is emitted. Bytecode seems to be the only sensible way (also for future maybe native JS type checking), but would love to see alternatives that are smaller in bundle size and faster in runtime performance.

M-jerez commented 11 months ago

Thanks for the reply @marcj

Seems there are many approaches to solve this issue and are pretty incompatible. Having a single library to cover all of them would be an herculean task.

at least we have a cross reference issue on the involver repos now.

Feel free to close the issue and thanks again for the reply 👍

M-jerez commented 11 months ago

closing this as seem it is not possible, thanks for looking at it 👍