Fumadocs TypeScript Integration
fumadocs-typescript is a specialized integration package within the Fumadocs ecosystem, providing robust TypeScript support for documentation projects built with Next.js. It enables developers to define and enforce type-safety for MDX frontmatter, ensuring consistency and reducing errors in content authoring. The package is currently at version 5.2.6. While its specific release cadence isn't independently clear, it often updates in conjunction with major releases of `fumadocs-core` and `fumadocs-ui`, which demonstrate frequent maintenance and improvements. Its key differentiator is its seamless integration with the Fumadocs MDX processing pipeline, allowing for automatic type generation and validation of content-level metadata, crucial for large-scale documentation sites. This significantly enhances the developer experience by providing autocompletion and compile-time checks for content properties.
Common errors
-
Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a CommonJS (`.js`) Next.js configuration file, or vice versa.fixRename your `next.config.js` to `next.config.mjs` and ensure all module imports within it use `import` syntax. If intentionally using CommonJS, use `require()`. -
Property 'someField' does not exist on type 'Frontmatter'.
cause The generated `Frontmatter` type does not include `someField`, likely because it's missing from your schema definition in `next.config.mjs`, or the types are outdated.fixCheck your `myFrontmatterSchema` definition in `next.config.mjs` and add `someField` with its appropriate type. Restart your development server or trigger a build to regenerate the types. Verify `tsconfig.json` includes the generated types path. -
Error: next-mdx-remote failed to parse MDX. If you're using a custom loader, ensure it returns valid MDX.
cause Incorrect configuration of `fumadocs-typescript` or `@fumadocs/mdx` within `next.config.mjs`, preventing proper processing of MDX files.fixReview the `next.config.mjs` quickstart example and your setup. Ensure `createLoader` from `fumadocs-typescript/loader` is correctly applied and chained with `createMDX` from `@fumadocs/mdx`. Check for typos in schema definitions.
Warnings
- breaking Fumadocs, including `fumadocs-typescript` when used in Next.js projects, increasingly expects Next.js configuration files to be ESM-only. Ensure your `next.config.js` is renamed to `next.config.mjs` to avoid module resolution errors.
- gotcha This package has strict peer dependencies on `fumadocs-core`, `fumadocs-ui`, `react`, and `react-dom`. Mismatched versions can lead to runtime errors or unexpected behavior, especially with major version bumps.
- gotcha For TypeScript to recognize the generated frontmatter types (e.g., from `.fumadocs/types.d.ts`), you must ensure the generated file's path is included in your `tsconfig.json`'s `include` array.
- gotcha When defining your frontmatter schema, pay close attention to the `required` property. If a field is marked as `required: true` but is missing from an MDX file's frontmatter, it will result in a build error.
Install
-
npm install fumadocs-typescript -
yarn add fumadocs-typescript -
pnpm add fumadocs-typescript
Imports
- createLoader
const createLoader = require('fumadocs-typescript/loader');import { createLoader } from 'fumadocs-typescript/loader'; - Frontmatter (generated type)
import type { Frontmatter } from 'fumadocs-typescript';import type { Frontmatter } from './.fumadocs/types';
Quickstart
// next.config.mjs
import { createLoader } from 'fumadocs-typescript/loader';
import { createMDX } from '@fumadocs/mdx'; // A common companion package in Fumadocs projects
// Define your frontmatter schema here. This schema will be used by
// fumadocs-typescript to generate corresponding TypeScript types for your MDX content.
const myFrontmatterSchema = {
title: { type: 'string', required: true, description: 'The title of the document' },
description: { type: 'string', optional: true, description: 'A brief summary of the content' },
date: { type: 'string', optional: true, format: 'date', description: 'Publication date (YYYY-MM-DD)' },
tags: { type: 'array', items: { type: 'string' }, optional: true, description: 'Categorization tags' },
category: { type: 'enum', enum: ['guides', 'api', 'tutorials'], optional: true, description: 'Content category' }
};
// Initialize the Fumadocs TypeScript loader with your schema.
// This loader processes the schema to generate a `types.d.ts` file,
// enabling type-safe access to frontmatter in your components.
const withFumadocsTypescript = createLoader({
schema: myFrontmatterSchema,
// By default, types are generated in .fumadocs/types.d.ts. You can customize the output path:
// outputPath: './custom-types/my-fumadocs-types.d.ts',
});
// Integrate with the main Fumadocs MDX loader for content processing.
// This is a typical setup in Fumadocs projects, chaining the loaders.
const withFumadocsMDX = createMDX({
content: {
// Configure where your MDX content is located, e.g., all files under the 'content' directory
root: 'content',
},
});
/** @type {import('next').NextConfig} */
const nextConfig = {
// Your standard Next.js configuration options go here
reactStrictMode: true,
// Add any other Next.js specific configurations
};
// Chain the loaders: the TypeScript loader typically runs 'before' or in conjunction
// with the MDX loader to ensure types are available for content processing.
export default withFumadocsMDX(withFumadocsTypescript(nextConfig));
// --- Example MDX file (content/getting-started.mdx) ---
// ---
// title: Getting Started with Fumadocs
// description: Learn how to set up your first Fumadocs project with TypeScript.
// date: 2023-10-26
// tags: ["guide", "setup", "typescript"]
// category: "guides"
// ---
// # Hello Fumadocs!
// This is your first document using type-safe frontmatter.
// Access `frontmatter.title` in your React components with full TypeScript support.