Skip to content

lazyPlugins() return type breaks defineConfig under exactOptionalPropertyTypes #3597

In review

Description

@luizfneves404

Describe the bug

lazyPlugins() is typed PluginOption[] | undefined (it returns undefined when VP_RESOLVING_CONFIG_METADATA=1), so the documented defineConfig({ plugins: lazyPlugins(...) }) fails to type-check with exactOptionalPropertyTypes: true. Workaround: lazyPlugins(...) ?? []. Suggested fix: return [] in metadata mode and type it PluginOption[].

Reproduction

https://gist.github.com/luizfneves404/2bea237980ca04546928f013b06673a5

Steps to reproduce

pnpm install
pnpm exec tsc -p tsconfig.json
vite.config.ts(3,29): error TS2345: Argument of type '{ plugins: PluginOption[] | undefined; }' is not assignable to parameter of type 'ViteUserConfigExport'.
  Type '{ plugins: PluginOption[] | undefined; }' is not assignable to type 'UserConfig' with 'exactOptionalPropertyTypes: true'. ...
        Type 'undefined' is not assignable to type 'PluginOption[]'.

System Info

vp v1.8.0 (vite-plus 1.8.0), TypeScript 7.0.2
Node.js v26.13.1, Linux

Used Package Manager

pnpm

Validations

  • Read the Contributing Guidelines.
  • Check that there isn't already an issue for the same bug.
  • Confirm this is a Vite+ issue and not an upstream issue.
  • The provided reproduction is a minimal reproducible example.

Disclosure: written by an AI agent (Claude Code), reviewed and filed by me.

Activity

  1. added theissue type on Nov 28, 2026
  2. voidzero-bot commented on Nov 28, 2026

    @voidzero-bot
    Contributor
    • Reproduced: Yes
    • Exploration: Yes — View branch
    • Unit Test: Yes — packages/cli/src/tests/index.spec.ts
    • Priority: Priority P2: Has Workaround. The documented plugins: lazyPlugins(...) pattern fails to type-check, but only for projects using exactOptionalPropertyTypes: true, and lazyPlugins(...) ?? [] is a straightforward workaround. The bug is in vite-plus-owned public types and also affects vp migrate output, so it is worth fixing promptly but is not breaking installs or builds.

    The root cause is in packages/cli/src/define-config.ts:1148-1152: both lazyPlugins() overloads are typed Array<PluginOption> | undefined because the implementation returns undefined as an internal runtime sentinel while vite-plus resolves config metadata (VP_RESOLVING_CONFIG_METADATA=1). That sentinel leaked into the public return type, and Vite's UserConfig.plugins is optional without undefined, so under exactOptionalPropertyTypes: true the object literal { plugins: PluginOption[] | undefined } is rejected with TS2345. This was never covered because the in-repo type-compat tests do not enable that flag; the undefined return has been part of the type since lazyPlugins was introduced in #1215.

    I found a potential fix for this issue. In metadata mode, lazyPlugins now returns [] instead of undefined, and both overloads are typed Array<PluginOption>, exactly as suggested in the report — internally the value was already normalized with config.plugins ?? [], so plugin-skipping behavior is unchanged. A compile-time regression assertion was added so the build fails if undefined ever re-enters the public return type. This needs human review before merging.

    Full Triage Report

    Triage Report: gh-3597 — lazyPlugins() return type breaks defineConfig under exactOptionalPropertyTypes

    Issue Summary

    lazyPlugins() is declared as returning Array<PluginOption> | undefined because it returns undefined while vite-plus resolves config metadata (VP_RESOLVING_CONFIG_METADATA=1). The documented usage defineConfig({ plugins: lazyPlugins(() => [...]) }) therefore fails to type-check in projects using "exactOptionalPropertyTypes": true, where the optional plugins?: PluginOption[] property of Vite's UserConfig cannot accept an explicit undefined.

    • Expected behavior: defineConfig({ plugins: lazyPlugins(...) }) type-checks under exactOptionalPropertyTypes (reporter suggests returning [] in metadata mode and typing the helper as PluginOption[]).
    • Actual behavior: tsc reports TS2345: 'undefined' is not assignable to type 'PluginOption[]'.

    Environment

    Component Version
    Vite+ (vp global / local vite-plus) v1.8.0 (built from current checkout main)
    Node.js v24.22.3
    pnpm v12.11.2
    TypeScript (in repro) 7.0.2 (same as reporter)
    vite (in repro) 8.4.0
    OS Linux x86_64 (Ubuntu runner)

    Reproduction

    Minimal project linked to the checkout via pnpm link .../packages/cli, with tsconfig.json: strict: true, exactOptionalPropertyTypes: true, skipLibCheck: true, noEmit: true, and:

    import { defineConfig, lazyPlugins } from 'vite-plus';
    
    export default defineConfig({
      plugins: lazyPlugins(() => []),
    });

    Commands attempted

    1. Trigger (reported usage): tsc -p tsconfig.json → exit 1:

      vite.config.ts(3,29): error TS2345: Argument of type '{ plugins: PluginOption[] | undefined; }' is not assignable to parameter of type 'ViteUserConfigExport'.
        Type '{ plugins: PluginOption[] | undefined; }' is not assignable to type 'UserConfig' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
          Types of property 'plugins' are incompatible.
            Type 'PluginOption[] | undefined' is not assignable to type 'PluginOption[]'.
            Type 'undefined' is not assignable to type 'PluginOption[]'.
      

      This matches the error in the issue verbatim.

    2. Control A (reported workaround): plugins: lazyPlugins(() => []) ?? [] → exit 0.

    3. Control B (flag isolated): same config with exactOptionalPropertyTypes: false → exit 0.

    4. Control C (config shape isolated): plain plugins: [] with exactOptionalPropertyTypes: true → exit 0.

    Result

    Reproduced. The reported TS2345 occurs with the current checkout's built vite-plus v1.8.0 and TypeScript 7.0.2. All four controls isolate the failure to the undefined in lazyPlugins's return type combined with the exactOptionalPropertyTypes flag.

    Diagnosis

    Layer

    Local JS CLI package surface: packages/cli (config helper types), not the global Rust CLI. lazyPlugins and defineConfig are exported from packages/cli/src/define-config.ts and shipped through packages/cli/dist/define-config-*.d.ts.

    Root cause

    packages/cli/src/define-config.ts:1148-1152 — both lazyPlugins overloads are declared as returning Array<PluginOption> | undefined, and the implementation returns undefined when process.env[CONFIG_METADATA_ENV] === '1' (CONFIG_METADATA_ENV is VP_RESOLVING_CONFIG_METADATA, packages/cli/src/utils/constants.ts:145). The consumer side is Vite's UserConfig (upstream vite/packages/vite/src/node/config.ts:414), whose plugin field is optional without an explicit undefined member: plugins?: PluginOption[]. Under exactOptionalPropertyTypes, an optional property may be omitted, but not assigned undefined — exactly the TS2345 observed.

    Under default strict (without exactOptionalPropertyTypes), undefined is assignable to optional properties, so the documented pattern compiles — which is why the existing type-compat tests in packages/cli/src/__tests__/index.spec.ts never caught it. Both the sync and async overloads have the problem.

    Why returning [] is runtime-safe

    • injectPluginIntoInlineConfig (define-config.ts:894-912) already normalizes the field with config.plugins ?? [] before injecting vite-plus' internal plugins, so [] and undefined are indistinguishable there.
    • Vite itself flattens plugin arrays and filters falsy entries, so plugins: [] behaves the same as plugins: undefined during config load.
    • The Rust migration rewriter (crates/vp_migration/src/vite_config.rs:375+) and the static config extractor detect lazyPlugins syntactically; none depend on the runtime undefined return value.

    Edge cases

    • The skip signal remains observable via process.env.VP_RESOLVING_CONFIG_METADATA; no code in the repository branches on the undefined return value.
    • Narrowing the public return type to PluginOption[] is source-compatible for the documented usage, including the ?? [] workaround.
    • The same single early-return covers both sync and async factories in metadata mode.

    Regression evidence

    Not a regression. The | undefined return type was present since the helper's introduction in #1215 (abc3b2a) and retained when the skip condition changed in #1939 (181e81d). The in-repo type-compat tests never enabled exactOptionalPropertyTypes, so the incompatibility was never covered.

    Confidence

    high — the failing declaration and the optional-without-undefined target property are proven by code inspection, and the reproduction plus three controls isolate the cause exactly.

    Verification (bug vs intended)

    • Documented API contract: docs/guide/troubleshooting.md:93-112 presents plugins: lazyPlugins(...) — sync and async — inside defineConfig as the usage of the helper.
    • Vite+ generates this pattern itself: vp migrate rewrites inline Vite plugin arrays to plugins: lazyPlugins(() => [...]), so vite-plus' own tooling emits a config that does not type-check under this standard strict flag combination.
    • No expressed intent for undefined in the public type: the undefined return is an internal runtime sentinel; neither feat: add lazyPlugins() helper for conditional plugin loading #1215 nor fix(cli): skip lazyPlugins only while resolving config metadata #1939 mentions the public return type, no RFC covers it, and no file in the repository mentions exactOptionalPropertyTypes.
    • Not upstream-forwarded behavior: docs/guide/migrate-rules.md:208 states vite-plus owns lazyPlugins.

    Verdict: bug — the shipped return type contradicts the documented (and migrator-generated) usage of the API under a common TypeScript strictness setting.

    Fix

    All fix-stage preconditions held (reproduced, high-confidence diagnosis, bug verdict, vite-plus-owned code, no product decision needed, no assignees), so implementation proceeded.

    Changed files

    1. packages/cli/src/define-config.ts — both lazyPlugins overloads (and the implementation signature) now return Array<PluginOption>; the metadata-mode early return yields [] instead of undefined, with a comment explaining why.

    2. packages/cli/src/__tests__/index.spec.ts — updated the four metadata-mode assertions from toBeUndefined() to toEqual([]); removed now-unneeded non-null assertions; initialized during (= []) to satisfy definite-assignment analysis (TS2454) once the return type excludes undefined; new regression test — a compile-time assertion plus runtime check that the public return type never includes undefined:

      const lazyPluginsReturnExcludesUndefined: [undefined] extends [ReturnType<typeof lazyPlugins>]
        ? never
        : true = true;

      Under the old declaration this annotation resolves to never and fails to compile (TS2322: Type 'true' is not assignable to type 'never'); with the fix it is true.

    Regression-test evidence (fails without the fix)

    With the source change temporarily reverted and the spec change in place, pnpm tsgo failed with TS2322: Type 'true' is not assignable to type 'never' in the spec, and the linked reproduction failed with the original TS2345. Both pass after restoring the fix.

    Validation

    Check Result
    pnpm -F vite-plus build-ts Rebuilt; shipped .d.ts now declares lazyPlugins(...): Array<PluginOption>.
    Original reproduction (exactOptionalPropertyTypes: true, sync) exit 0 (was TS2345)
    Control: reported workaround ?? [] exit 0
    Async overload under exactOptionalPropertyTypes exit 0
    pnpm tsgo (repo type check incl. spec files) No errors from changed files; the only remaining error is pre-existing/environmental (docs/_data/team.ts, vitepress not installed), identical on the clean tree.
    vp test packages/cli/src/__tests__/index.spec.ts 18/18 passed (incl. the new regression test)
    Neighboring config-resolution specs 152/152 passed
    vp test packages/cli (full CLI unit suite) 3480 passed, 1 skipped, 101 files
    vp check (fmt + type-aware lint) 0 findings in changed files; remaining errors are pre-existing docs/ errors, identical count on the clean tree.
    Runtime smoke (linked repro): metadata mode VP_RESOLVING_CONFIG_METADATA=1 → returns [] for sync and async factories (factory not called); default mode returns the plugins array.
    Rust checks / snapshot tests Skipped — TypeScript-only change; no CLI output or interaction changed.

    Alternatives considered

    • Widen defineConfig's accepted config type: rejected — loosens the public type contract for all users to paper over an internal sentinel.
    • Keep undefined in the type but return [] at runtime: rejected — the public type would still break the documented usage, which is the reported bug.
    • Enable exactOptionalPropertyTypes repo-wide: rejected as out of scope; the compile-time regression assertion covers the contract.

    Notes for release

    The public return type narrows from PluginOption[] | undefined to PluginOption[]. Source compatibility is preserved for documented usage (including the ?? [] workaround). Undocumented code that detected metadata mode by checking the return value for undefined (instead of reading VP_RESOLVING_CONFIG_METADATA) would observe []; no such usage exists in the repository. vp migrate output and docs/guide/troubleshooting.md examples are unchanged and now type-check under exactOptionalPropertyTypes.

    This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.

    I've opened a pull request with this fix: #3598

  3. added
    - P2: has workaround
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P2: has workaroundtriage: fix verified

    Type

    Fields

    Priority

    None yet

    Effort

    None yet

    Target date

    None yet

    Start date

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions