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.
## 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<ThemeConfig>({
export default defineConfigWithTheme<ThemeConfig>({
themeConfig: {
// Type is `ThemeConfig`
// Type is `ThemeConfig`
}
}, true); // declare `usingCustomTheme` and discard usage of the default theme.
})
```

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

4
types/shared.d.ts vendored

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

Loading…
Cancel
Save