Skip to Content
Vuesho

Type Tables

Generate tables from TypeScript types.

@vuesho/typescript generates a table from a type's properties or a Vue component's props, when the site builds. Doc comments become the descriptions:

<auto-type-table path="../src/button.ts" name="ButtonProps" />

Prop

Type

A row opens on click to show its full type and description.

Setup

Install the Packages

The package needs TypeScript 6's JavaScript API, which TypeScript 7 doesn't have:

npm i -D @vuesho/typescript typescript@6

Add the Plugin

vuesho.config.ts
import { autoTypeTablePlugin } from "@vuesho/typescript"; 

createSite(docs.toSource()).plugins(
  recommendedPreset(),
  autoTypeTablePlugin(), 
);

Attributes

<auto-type-table> renders the type name exported from the file at path, as a TypeTable. Other attributes, like class and id, go on the table:

Prop

Type

Write path, name, type and id as plain attributes, since a bound one, like :path, makes the page fail to compile. A type error fails the build at the tag's line, with TypeScript's message.

In the page's Markdown version, each type table is a Markdown table with the full types. Search indexes each row.

Inline Types

type takes a type instead of a file. With path, the code in type is added to the end of the file, and name names a type in it:

<auto-type-table type="{ size: number; label?: string }" />

<auto-type-table
  path="../src/button.ts"
  type="import type { Icon } from './icon';
export type IconButtonProps = ButtonProps & { icon: Icon };"
  name="IconButtonProps"
/>

Vue Components

When path points to a .vue file, the table lists the component's props, without the attributes every element takes, like class. name names an export, and defaults to the default export.

<auto-type-table path="../src/components/Button.vue" />

Doc Comments

A description is Markdown. {@link} and {@linkplain} render as their text, or the name they link. {@linkcode} renders as code. A link to a URL renders as a link. These tags change a row:

TagEffect
@defaultValue, @defaultSets the default value
@deprecatedStrikes through the property's name
@param, @returnsDescribes a function's parameters and result
@internalLeaves the property out of the table
@remarksInline code at its start, like @remarks `Size` , sets the short type
@fumadocsTypeInline code sets the full type
@fumadocsHrefLinks the type to a URL

@example and @see aren't shown in the description. transform can read them from an entry's tags and append them to it.

Custom Pages

In a custom page, read the properties with generateTypeTable() on a generator from createGenerator(), and display them with TypeTable. The path that generateTypeTable() takes is relative to the working directory:

src/server/types.ts
import { createGenerator } from "@vuesho/typescript";
import type { TypeNode } from "vuesho/ui/components/type-table";

const generator = createGenerator();

export function buttonProps(): Record<string, TypeNode> {
  const [doc] = generator.generateTypeTable({ path: "src/button.ts", name: "ButtonProps" });
  const type: Record<string, TypeNode> = {};
  for (const entry of doc!.entries) {
    type[entry.name] = {
      type: entry.simplifiedType,
      typeDescription: entry.type,
      // The doc comment, as plain text.
      description: entry.description,
      default: entry.tags.find((tag) => tag.name === "defaultValue")?.text,
      required: entry.required,
      deprecated: entry.deprecated,
    };
  }
  return type;
}
src/pages/button.vue
<script setup lang="ts">
import { definePage } from "vuesho/client";
import { TypeTable } from "vuesho/components";
import type { TypeNode } from "vuesho/ui/components/type-table";
import { buttonProps } from "../server/types";

definePage({
  data: { title: "Button" },
  // Runs when the site builds.
  props: () => ({ type: buttonProps() }),
});
defineProps<{ type: Record<string, TypeNode> }>();
</script>

<template>
  <TypeTable :type="type" />
</template>

Options

Prop

Type

The generator reads the tsconfig.json in the working directory, unless tsconfigPath points to another.

Short Types

A closed row shows a short type, like union, and opens to show the full type. typeSimplifier writes the short types. This one keeps the full type when it's short:

vuesho.config.ts
import { autoTypeTablePlugin } from "@vuesho/typescript";

createSite(docs.toSource()).plugins(
  recommendedPreset(),
  autoTypeTablePlugin({
    typeSimplifier: {
      override({ type, checker, location }) {
        const text = checker.typeToString(type, location).replace(/ \| undefined$/, "");
        if (text.length <= 32 && !text.includes("=>")) return text;
      },
    },
  }),
);

Caching

generatorFileCache() saves the tables in node_modules/.vuesho/typescript, or in the folder of dir, so the next build and vuesho dev read them instead of running TypeScript. A cached table is generated again when its file, a file it imports, or an option other than a function changes:

vuesho.config.ts
import { autoTypeTablePlugin, createGenerator, generatorFileCache } from "@vuesho/typescript";

const generator = createGenerator({ cache: generatorFileCache() });

createSite(docs.toSource()).plugins(
  recommendedPreset(),
  autoTypeTablePlugin({ generator }), 
);