Suporte a TypeScript
Docusaurus is written in TypeScript and provides first-class TypeScript support.
The minimum required version is TypeScript 5.0.
Inicialização
O Docusaurus suporta escrever e usar componentes de temas TypeScript. If the init template provides a TypeScript variant, you can directly initialize a site with full TypeScript support by using the --typescript
flag.
npx create-docusaurus@latest my-website classic --typescript
Below are some guides on how to migrate an existing project to TypeScript.
Configuração
Add the following packages to your project:
- npm
- Yarn
- pnpm
npm install --save-dev typescript @docusaurus/module-type-aliases @docusaurus/tsconfig @docusaurus/types
yarn add --dev typescript @docusaurus/module-type-aliases @docusaurus/tsconfig @docusaurus/types
pnpm add --save-dev typescript @docusaurus/module-type-aliases @docusaurus/tsconfig @docusaurus/types
Em seguida, adicione o tsconfig.json
à raiz do seu projeto com o seguinte conteúdo:
{
"extends": "@docusaurus/tsconfig",
"compilerOptions": {
"baseUrl": "."
}
}
O Docusaurus não usa este tsconfig.json
para compilar seu projeto. É adicionado apenas para uma experiência do Editor mais agradável, embora você possa optar por executar tsc
para digitar seu código para si mesmo ou no CI.
Agora você pode começar a escrever componentes de tema TypeScript.
Typing the config file
It is possible to use a TypeScript config file in Docusaurus.
import type {Config} from '@docusaurus/types';
import type * as Preset from '@docusaurus/preset-classic';
const config: Config = {
title: 'My Site',
favicon: 'img/favicon.ico',
/* Your site config here */
presets: [
[
'classic',
{
/* Your preset config here */
} satisfies Preset.Options,
],
],
themeConfig: {
/* Your theme config here */
} satisfies Preset.ThemeConfig,
};
export default config;
It is also possible to use JSDoc type annotations within a .js
file:
By default, the Docusaurus TypeScript config does not type-check JavaScript files.
The // @ts-check
comment ensures the config file is properly type-checked when running npx tsc
.
// @ts-check
/** @type {import('@docusaurus/types').Config} */
const config = {
tagline: 'Dinosaurs are cool',
favicon: 'img/favicon.ico',
/* Your site config here */
presets: [
[
'@docusaurus/preset-classic',
/** @type {import('@docusaurus/preset-classic').Options} */
(
{
/* Your preset config here */
}
),
],
],
themeConfig:
/** @type {import('@docusaurus/preset-classic').ThemeConfig} */
(
{
/* Your theme config here */
}
),
};
export default config;
Type annotations are very useful and help your IDE understand the type of config objects!
The best IDEs (VS Code, WebStorm, IntelliJ...) will provide a nice auto-completion experience.
Componentes de tema TypeScript Swizzling
For themes that support TypeScript theme components, you can add the --typescript
flag to the end of the swizzle
command to get TypeScript source code. Por exemplo, o seguinte comando irá gerar index.tsx
e styles.module.css
em src/theme/Footer
.
- npm
- Yarn
- pnpm
npm run swizzle @docusaurus/theme-classic Footer -- --typescript
yarn swizzle @docusaurus/theme-classic Footer --typescript
pnpm run swizzle @docusaurus/theme-classic Footer -- --typescript
All official Docusaurus themes support TypeScript theme components, including theme-classic
, theme-live-codeblock
, and theme-search-algolia
. If you are a Docusaurus theme package author who wants to add TypeScript support, see the Lifecycle APIs docs.