Recurse through TypeScript type and output final “materialized” version

42 views Asked by At

Is there a library or CLI tool which can be used to emit TypeScript types “expanded” as far as the compiler can infer. For example, for:

type MyString = string;

export type MyObject = {
  str: MyString;
};

…is there a way I can get to a declaration file with:

export type MyObject = {
  str: string;
}

Ultimately I have some zod schemas and would like to output a declaration file with “materialized types”.

eg. for the following:

import { z } from "zod";

const Foo = z.object({
  bar: z.string(),
});

export type Bar = z.infer<typeof Foo>;

The .d.ts file would contain:

export type Bar = z.infer<typeof Foo>;

Is there a method by which I can “expand” the type to output a version like:

export type Bar = {
  bar: string;
};

Edit - my question was marked as a duplicate of this one, but I’m specifically looking to output a .d.ts file with these expanded types that I can consume in other projects. That question just allows me to hover over the type in Visual Studio Code.

0

There are 0 answers