Skip to content

Instantly share code, notes, and snippets.

@SanariSan
Created November 26, 2024 17:48
Show Gist options
  • Save SanariSan/77303a896b27cc82105ce1c9f9d11547 to your computer and use it in GitHub Desktop.
Save SanariSan/77303a896b27cc82105ce1c9f9d11547 to your computer and use it in GitHub Desktop.
Prettify, show full nested ts type

tsconfig.json This prevents typehints for big types from showing ... N more ...

{
  "compilerOptions": {
    "noErrorTruncation": true,
  }
}

Make nested named types show up as their actual raw verion

type Primitive = string | number | boolean | null | undefined;

type Prettify<T> = {
  [K in keyof T]: T[K] extends Primitive
    ? T[K]
    : T[K] extends Array<any>
      ? T[K] extends Array<infer U>
        ? Array<Prettify<U>>
        : T[K]
      : T[K] extends object
        ? Prettify<T[K]>
        : T[K];
} & {};

type B = {
  c: number;
}

/**
 * Will be shown in typehint as:
 *
 * type A = {
 *  field: B;
 * }
 */
type A = {
  field: B
}

/**
 * Will be shown in typehint as:
 *
 * type RawA = {
 *  field: {
 *    c: number;
 *  };
 * }
 */
type RawA = Prettify<A>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment