Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot edit the config after calling defineConfig anymore #2524

Closed
4 tasks done
ST-DDT opened this issue Jun 19, 2023 · 3 comments · Fixed by #2529
Closed
4 tasks done

Cannot edit the config after calling defineConfig anymore #2524

ST-DDT opened this issue Jun 19, 2023 · 3 comments · Fixed by #2529
Labels
types Related to typings only

Comments

@ST-DDT
Copy link

ST-DDT commented Jun 19, 2023

Describe the bug

After the update from 1.0.0-beta.1 to 1.0.0-beta.2 I get a type error when trying to alter the config:

Reproduction

const config = defineConfig({ ... });

if (versionBannerInfix) {
  config.head?.push([
    'script',
    { id: 'restore-banner-preference' },
    `someScript()`,
  ]);
}

export default config;

Working reproduction: https://github.com/faker-js/faker/blob/a6f5a39fff65d8923aab6db8a05a3ab79d097896/docs/.vitepress/config.ts#L257-L272

Expected behavior

I expect it to still work.

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (16) x64 Intel(R) Core(TM) i7-7820X CPU @ 3.60GHz
    Memory: 52.73 GB / 63.69 GB
  Binaries:
    Node: 18.16.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.16.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.1848.0), Chromium (114.0.1823.51)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    vitepress: 1.0.0-beta.2 => 1.0.0-beta.2

Additional context

Found here: faker-js/faker#2216

// Vitepress Types
type UserConfigExport<ThemeConfig> = UserConfig<ThemeConfig> | Promise<UserConfig<ThemeConfig>> | UserConfigFn<ThemeConfig>;

declare function defineConfig(config: UserConfigExport<DefaultTheme.Config>): UserConfigExport<DefaultTheme.Config>;

The first UserConfigExport variant is a somewhat special case as it is the only that makes sense to change afterwards.

Fix:

Export two signatures:

declare function defineConfig(config: UserConfig<DefaultTheme.Config>): UserConfig<DefaultTheme.Config>;
declare function defineConfig(config: UserConfigExport<DefaultTheme.Config>): UserConfigExport<DefaultTheme.Config>;

Validations

@ST-DDT ST-DDT added the bug: pending triage Maybe a bug, waiting for confirmation label Jun 19, 2023
@brc-dd
Copy link
Member

brc-dd commented Jun 19, 2023

Ah yeah. We are aware of this. I'll push the changes with overloads tomorrow. It broke typings in some other plugins too. If it's blocking, you can use ts-ignore for now. (Only the types were broken, the functionality is same as earlier.)

@brc-dd brc-dd added types Related to typings only and removed bug: pending triage Maybe a bug, waiting for confirmation labels Jun 19, 2023
@brc-dd brc-dd self-assigned this Jun 19, 2023
@ST-DDT
Copy link
Author

ST-DDT commented Jun 19, 2023

If it's blocking

It doesn't really block us, only the dependency update.

@brc-dd
Copy link
Member

brc-dd commented Jun 20, 2023

Okay, so what you suggested, and what I had initially in mind won't work. It breaks typings for patterns like these:

defineConfig(() => ({ // <-- can be async, doesn't work unless you add empty parameter (_) or ({})
  // complex stuff here (like our own docs config)
}))

There was an attempt at narrowing types in Vite too: vitejs/vite#12021, vitejs/vite#12077 -- that broke typings and intellisense too 👀

TS is getting kind of confused between promises and functions.

At the moment I'd recommend you to directly import UserConfig and DefaultTheme:

import type { UserConfig, DefaultTheme } from 'vitepress'

const config: UserConfig<DefaultTheme.Config>  = { ... }

@brc-dd brc-dd removed their assignment Jun 20, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
types Related to typings only
Projects
None yet
2 participants