Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One Typescript Transformer to run them All #809

Closed
M-jerez opened this issue Sep 10, 2023 · 2 comments
Closed

One Typescript Transformer to run them All #809

M-jerez opened this issue Sep 10, 2023 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@M-jerez
Copy link

M-jerez commented Sep 10, 2023

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:

@samchon
Copy link
Owner

samchon commented Oct 5, 2023

As deepkit is not using ts-patch and performing transformation by itself, no way to support both of them.

Furthermore, besides typia transforms function call statements, deepkit writes special statements to target type directly.

Therefore, both typia and deepkit can't be compatible.

Typia

https://typia.io/docs/validators/assert

Deepkit

TypeScript Source

import { is } from '@deepkit/type';

class Something {
    public constructor(public readonly value: number) {}
}

console.log(is<Something>(new Something(5)));

Compiled JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const type_1 = require("@deepkit/type");
class Something {
    constructor(value) {
        this.value = value;
    }
}
Something.__type = ['value', 'constructor', 'P\'2!:9"0"5'];
console.log((0, type_1.is)(new Something(5)));

@samchon samchon closed this as not planned Won't fix, can't repro, duplicate, stale Oct 5, 2023
@AlexRMU
Copy link
Contributor

AlexRMU commented Aug 14, 2024

What about #878 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants