.armaan.s..
Deno17mo ago
.armaan.s.

Extract TypeScript types as JSON?

I have a Hono server and I want to extract / save the server routes type to either a .d.ts or a .json file. Ideally I would automate it using CLI commands but I can't even manually copy the types out of VSCode because the type get's truncated:

type RoutesType = {
    [x: `/google/${string}`]: {
        $get: {
            input: {};
            output: {
                token: {
                    token: string;
                    expires_in: number;
                } | undefined;
                grantedScopes: string[] | undefined;
                user: {
                    id?: string | undefined;
                    email?: string | undefined;
                    ... 5 more ...;
                    locale?: string | undefined;
                } | undefined;
            };
            outputFormat: "json";
            status: StatusCode;
        };
    };
    ... 11 more ...;
    "/static/*": {
        ...;
    };
}


When you hover over the type in VSCode, most of the route types are hidden by ellipses. Is there a way to see the full type and is there a way to convert it to JSON, in a format like:

{
  "output": {
    "token": {
      "OR": [
         {
           "token": "string",
           "expires_in": "number",
         },
         "undefined"
      ]
    }
  }
}
Was this page helpful?