pull/465/head
Evan You 4 years ago
parent 712e8af2ff
commit 83f408d4b2

@ -24,7 +24,6 @@ module.exports = {
Check out the [Config Reference](/config/basics) for a full list of options. Check out the [Config Reference](/config/basics) for a full list of options.
## Config Intellisense ## Config Intellisense
Since VitePress ships with TypeScript typings, you can leverage your IDE's intellisense with jsdoc type hints: Since VitePress ships with TypeScript typings, you can leverage your IDE's intellisense with jsdoc type hints:
@ -52,12 +51,11 @@ export default defineConfig({
VitePress also directly supports TS config files. You can use `.vitepress/config.ts` with the `defineConfig` helper as well. VitePress also directly supports TS config files. You can use `.vitepress/config.ts` with the `defineConfig` helper as well.
## Typed Theme Config ## Typed Theme Config
By default, `defineConfig` helper leverages the theme config type from default theme: By default, `defineConfig` helper leverages the theme config type from default theme:
```js ```ts
import { defineConfig } from 'vitepress' import { defineConfig } from 'vitepress'
export default defineConfig({ export default defineConfig({
@ -67,15 +65,15 @@ export default defineConfig({
}) })
``` ```
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 ```ts
import { defineConfig } from 'vitepress' import { defineConfigWithTheme } from 'vitepress'
import { ThemeConfig } from 'your-theme' import { ThemeConfig } from 'your-theme'
export default defineConfig<ThemeConfig>({ export default defineConfigWithTheme<ThemeConfig>({
themeConfig: { themeConfig: {
// Type is `ThemeConfig` // Type is `ThemeConfig`
} }
}, true); // declare `usingCustomTheme` and discard usage of the default theme. })
``` ```

@ -15,7 +15,7 @@ import {
HeadConfig, HeadConfig,
LocaleConfig, LocaleConfig,
createLangDictionary, createLangDictionary,
DefaultTheme, DefaultTheme
} from './shared' } from './shared'
import { resolveAliases, APP_PATH, DEFAULT_THEME_PATH } from './alias' import { resolveAliases, APP_PATH, DEFAULT_THEME_PATH } from './alias'
import { MarkdownOptions } from './markdown/markdown' import { MarkdownOptions } from './markdown/markdown'
@ -27,16 +27,14 @@ const debug = _debug('vitepress:config')
export type { MarkdownOptions } export type { MarkdownOptions }
export type ThemeConfig = any; export interface UserConfig<ThemeConfig = any> {
extends?: RawConfigExports<ThemeConfig>
export interface UserConfig<T extends ThemeConfig = ThemeConfig> {
extends?: RawConfigExports<T>
lang?: string lang?: string
base?: string base?: string
title?: string title?: string
description?: string description?: string
head?: HeadConfig[] head?: HeadConfig[]
themeConfig?: T themeConfig?: ThemeConfig
locales?: Record<string, LocaleConfig> locales?: Record<string, LocaleConfig>
markdown?: MarkdownOptions markdown?: MarkdownOptions
/** /**
@ -58,15 +56,15 @@ export interface UserConfig<T extends ThemeConfig = ThemeConfig> {
mpa?: boolean mpa?: boolean
} }
export type RawConfigExports<T extends ThemeConfig = ThemeConfig> = export type RawConfigExports<ThemeConfig = any> =
| UserConfig<T> | UserConfig<ThemeConfig>
| Promise<UserConfig<T>> | Promise<UserConfig<ThemeConfig>>
| (() => UserConfig<T> | Promise<UserConfig<T>>) | (() => UserConfig<ThemeConfig> | Promise<UserConfig<ThemeConfig>>)
export interface SiteConfig<T = ThemeConfig> { export interface SiteConfig<ThemeConfig = any> {
root: string root: string
srcDir: string srcDir: string
site: SiteData<T> site: SiteData<ThemeConfig>
configPath: string | undefined configPath: string | undefined
themeDir: string themeDir: string
outDir: string outDir: string
@ -85,12 +83,16 @@ const resolve = (root: string, file: string) =>
/** /**
* Type config helper * Type config helper
*/ */
export function defineConfig<T extends ThemeConfig = ThemeConfig>( export function defineConfig(config: UserConfig<DefaultTheme.Config>) {
config: UserConfig<T>, return config
usingCustomTheme: true }
): void
export function defineConfig(config: UserConfig<DefaultTheme.Config>): void /**
export function defineConfig(config: ThemeConfig) { * Type config helper for custom theme config
*/
export function defineConfigWithTheme<ThemeConfig>(
config: UserConfig<ThemeConfig>
) {
return config return config
} }
@ -158,7 +160,7 @@ async function resolveUserConfig(
} }
} }
const userConfig: RawConfigExports<ThemeConfig> = configPath const userConfig: RawConfigExports = configPath
? (( ? ((
await loadConfigFromFile( await loadConfigFromFile(
{ {
@ -181,7 +183,7 @@ async function resolveUserConfig(
} }
async function resolveConfigExtends( async function resolveConfigExtends(
config: RawConfigExports<ThemeConfig> config: RawConfigExports
): Promise<UserConfig> { ): Promise<UserConfig> {
const resolved = await (typeof config === 'function' ? config() : config) const resolved = await (typeof config === 'function' ? config() : config)
if (resolved.extends) { if (resolved.extends) {

4
types/shared.d.ts vendored

@ -11,7 +11,7 @@ export interface LocaleConfig {
selectText?: string selectText?: string
} }
export interface SiteData<T = any> { export interface SiteData<ThemeConfig = any> {
base: string base: string
/** /**
* Language of the site as it should be set on the `html` element. * Language of the site as it should be set on the `html` element.
@ -21,7 +21,7 @@ export interface SiteData<T = any> {
title: string title: string
description: string description: string
head: HeadConfig[] head: HeadConfig[]
themeConfig: T themeConfig: ThemeConfig
locales: Record<string, LocaleConfig> locales: Record<string, LocaleConfig>
/** /**
* Available locales for the site when it has defined `locales` in its * Available locales for the site when it has defined `locales` in its

Loading…
Cancel
Save