diff --git a/docs/guide/configuration.md b/docs/guide/configuration.md index 4ec74081..8ffaad52 100644 --- a/docs/guide/configuration.md +++ b/docs/guide/configuration.md @@ -24,7 +24,6 @@ module.exports = { Check out the [Config Reference](/config/basics) for a full list of options. - ## Config Intellisense Since VitePress ships with TypeScript typings, you can leverage your IDE's intellisense with jsdoc type hints: @@ -52,30 +51,29 @@ export default defineConfig({ VitePress also directly supports TS config files. You can use `.vitepress/config.ts` with the `defineConfig` helper as well. - ## Typed Theme Config By default, `defineConfig` helper leverages the theme config type from default theme: -```js +```ts import { defineConfig } from 'vitepress' export default defineConfig({ themeConfig: { - // Type is `DefaultTheme.Config` + // Type is `DefaultTheme.Config` } }) ``` -If you use a custom theme, you'll be able to pass the generics type for your custom theme, and you need overload it with the second parameter of `defineConfig` helper: +If you use a custom theme and want type checks for the theme config, you'll need to use `defineConfigWithTheme` instead, and pass the config type for your custom theme via a generic argument: -```js -import { defineConfig } from 'vitepress' +```ts +import { defineConfigWithTheme } from 'vitepress' import { ThemeConfig } from 'your-theme' -export default defineConfig({ +export default defineConfigWithTheme({ themeConfig: { - // Type is `ThemeConfig` + // Type is `ThemeConfig` } -}, true); // declare `usingCustomTheme` and discard usage of the default theme. +}) ``` diff --git a/src/node/config.ts b/src/node/config.ts index 3c82976c..15ceaf72 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -15,7 +15,7 @@ import { HeadConfig, LocaleConfig, createLangDictionary, - DefaultTheme, + DefaultTheme } from './shared' import { resolveAliases, APP_PATH, DEFAULT_THEME_PATH } from './alias' import { MarkdownOptions } from './markdown/markdown' @@ -27,16 +27,14 @@ const debug = _debug('vitepress:config') export type { MarkdownOptions } -export type ThemeConfig = any; - -export interface UserConfig { - extends?: RawConfigExports +export interface UserConfig { + extends?: RawConfigExports lang?: string base?: string title?: string description?: string head?: HeadConfig[] - themeConfig?: T + themeConfig?: ThemeConfig locales?: Record markdown?: MarkdownOptions /** @@ -58,15 +56,15 @@ export interface UserConfig { mpa?: boolean } -export type RawConfigExports = - | UserConfig - | Promise> - | (() => UserConfig | Promise>) +export type RawConfigExports = + | UserConfig + | Promise> + | (() => UserConfig | Promise>) -export interface SiteConfig { +export interface SiteConfig { root: string srcDir: string - site: SiteData + site: SiteData configPath: string | undefined themeDir: string outDir: string @@ -85,12 +83,16 @@ const resolve = (root: string, file: string) => /** * Type config helper */ - export function defineConfig( - config: UserConfig, - usingCustomTheme: true -): void -export function defineConfig(config: UserConfig): void -export function defineConfig(config: ThemeConfig) { +export function defineConfig(config: UserConfig) { + return config +} + +/** + * Type config helper for custom theme config + */ +export function defineConfigWithTheme( + config: UserConfig +) { return config } @@ -158,7 +160,7 @@ async function resolveUserConfig( } } - const userConfig: RawConfigExports = configPath + const userConfig: RawConfigExports = configPath ? (( await loadConfigFromFile( { @@ -181,7 +183,7 @@ async function resolveUserConfig( } async function resolveConfigExtends( - config: RawConfigExports + config: RawConfigExports ): Promise { const resolved = await (typeof config === 'function' ? config() : config) if (resolved.extends) { diff --git a/types/shared.d.ts b/types/shared.d.ts index 63fbb624..2beaf53f 100644 --- a/types/shared.d.ts +++ b/types/shared.d.ts @@ -11,7 +11,7 @@ export interface LocaleConfig { selectText?: string } -export interface SiteData { +export interface SiteData { base: string /** * Language of the site as it should be set on the `html` element. @@ -21,7 +21,7 @@ export interface SiteData { title: string description: string head: HeadConfig[] - themeConfig: T + themeConfig: ThemeConfig locales: Record /** * Available locales for the site when it has defined `locales` in its