Skip to Content
Vuesho

Type Checking

Type-check your config and pages.

vue-tsc checks Markdown pages like .vue files. It reports errors like an unknown prop, a wrong value type or an undefined name at their line in the page:

content/index.md
---
title: Introduction
---

<Callout tpye="warn">
This can't be undone.
</Callout>
content/index.md(5,10): error TS2353: Object literal may only specify known properties,
and 'tpye' does not exist in type '{ readonly title?: string | undefined; ... }'.

A site created by create-vuesho already has the setup below. Run the check with npm run types:check.

Setup

Install the Packages

vue-tsc needs TypeScript 6's JavaScript API, which TypeScript 7 doesn't have:

npm i -D typescript@6 vue-tsc @types/node

Add a tsconfig.json

Vuesho's types need ES2023 or later, and the Node.js and Vite types:

tsconfig.json
{
  "compilerOptions": {
    "target": "ES2023",
    "lib": ["ES2023", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "strict": true,
    "noEmit": true,
    "skipLibCheck": true,
    // Checks JavaScript files, and `<script setup>` without `lang="ts"`.
    "allowJs": true,
    "checkJs": true,
    "types": ["node", "vite/client"],
  },
  "include": ["*.ts", "src", "content"],
  "vueCompilerOptions": {
    // Reads `.md` files as Vue. VitePress isn't needed.
    "vitePressExtensions": [".md"],
    // Reads a page as Vuesho compiles it.
    "plugins": ["vuesho/type-check"],
    // Reports a prop that a component doesn't declare.
    "checkUnknownProps": true,
    // Reports a component that the page can't resolve.
    "checkUnknownComponents": true,
    // Allows `data-*` attributes on every component.
    "dataAttributes": ["data-*"],
  },
}

Run the Check

package.json
{
  "scripts": {
    "postinstall": "vuesho prepare",
    "types:check": "vuesho prepare && vue-tsc --noEmit"
  }
}
npm run types:check

vuesho prepare writes the list of the components that pages use, which the type check and the editor read. Run it again after you add a component or change vuesho.config.ts, unless vuesho dev is running. The postinstall script runs it after each install.

Checked Syntax

vuesho/type-check checks the components and the expressions of each page, and skips code blocks and escaped text, like \{{ name }}. It types the path params of a page and the argument of its frontmatter function, and the params of a custom page from its file path.

Each page gets the components of src/components/content/, of its collection's folder and of the plugins. A component that the page imports takes priority over one from a folder.

Editor Support

The Vue extension for VS Code checks pages as you write them, after you turn it on for Markdown:

.vscode/settings.json
{
  "vue.server.includeLanguages": ["vue", "markdown"]
}

Custom Syntax

Some markdown-it plugins add syntax that looks like Vue, like KaTeX braces. List them in the vuesho/type-check options:

tsconfig.json
{
  "vueCompilerOptions": {
    "plugins": [
      {
        "name": "vuesho/type-check",
        // Each entry resolves from the folder of `tsconfig.json`.
        "markdownItPlugins": [
          "@mdit/plugin-katex#katex", // A named export of a package
          "markdown-it-footnote", // The default export of a package
          "./src/syntax.js", // A file
          ["markdown-it-container", "tip"], // A plugin with its options
        ],
      },
    ],
  },
}

Other Content Folders

For a collection outside content/, add its folder to include:

tsconfig.json
{
  "include": ["*.ts", "src", "content", "blog"],
}