You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
lazyPlugins() return type breaks defineConfig under exactOptionalPropertyTypes #3597
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[].
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.
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 withoutundefined, 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:
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.
Control A (reported workaround):plugins: lazyPlugins(() => []) ?? [] → exit 0.
Control B (flag isolated): same config with exactOptionalPropertyTypes: false → exit 0.
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.
Not upstream-forwarded behavior:docs/guide/migrate-rules.md:208 states vite-plus ownslazyPlugins.
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
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.
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:
constlazyPluginsReturnExcludesUndefined: [undefined]extends[ReturnType<typeoflazyPlugins>]
? 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.
Activity
voidzero-bot commentedon Nov 28, 2026
plugins: lazyPlugins(...)pattern fails to type-check, but only for projects usingexactOptionalPropertyTypes: true, andlazyPlugins(...) ?? []is a straightforward workaround. The bug is in vite-plus-owned public types and also affectsvp migrateoutput, 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: bothlazyPlugins()overloads are typedArray<PluginOption> | undefinedbecause the implementation returnsundefinedas 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'sUserConfig.pluginsis optional withoutundefined, so underexactOptionalPropertyTypes: truethe 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; theundefinedreturn has been part of the type sincelazyPluginswas introduced in #1215.I found a potential fix for this issue. In metadata mode,
lazyPluginsnow returns[]instead ofundefined, and both overloads are typedArray<PluginOption>, exactly as suggested in the report — internally the value was already normalized withconfig.plugins ?? [], so plugin-skipping behavior is unchanged. A compile-time regression assertion was added so the build fails ifundefinedever re-enters the public return type. This needs human review before merging.Full Triage Report
Triage Report: gh-3597 —
lazyPlugins()return type breaksdefineConfigunderexactOptionalPropertyTypesIssue Summary
lazyPlugins()is declared as returningArray<PluginOption> | undefinedbecause it returnsundefinedwhilevite-plusresolves config metadata (VP_RESOLVING_CONFIG_METADATA=1). The documented usagedefineConfig({ plugins: lazyPlugins(() => [...]) })therefore fails to type-check in projects using"exactOptionalPropertyTypes": true, where the optionalplugins?: PluginOption[]property of Vite'sUserConfigcannot accept an explicitundefined.defineConfig({ plugins: lazyPlugins(...) })type-checks underexactOptionalPropertyTypes(reporter suggests returning[]in metadata mode and typing the helper asPluginOption[]).tscreports TS2345:'undefined' is not assignable to type 'PluginOption[]'.Environment
vpglobal / localvite-plus)main)Reproduction
Minimal project linked to the checkout via
pnpm link .../packages/cli, withtsconfig.json:strict: true,exactOptionalPropertyTypes: true,skipLibCheck: true,noEmit: true, and:Commands attempted
Trigger (reported usage):
tsc -p tsconfig.json→ exit 1:This matches the error in the issue verbatim.
Control A (reported workaround):
plugins: lazyPlugins(() => []) ?? []→ exit 0.Control B (flag isolated): same config with
exactOptionalPropertyTypes: false→ exit 0.Control C (config shape isolated): plain
plugins: []withexactOptionalPropertyTypes: true→ exit 0.Result
Reproduced. The reported TS2345 occurs with the current checkout's built
vite-plusv1.8.0 and TypeScript 7.0.2. All four controls isolate the failure to theundefinedinlazyPlugins's return type combined with theexactOptionalPropertyTypesflag.Diagnosis
Layer
Local JS CLI package surface:
packages/cli(config helper types), not the global Rust CLI.lazyPluginsanddefineConfigare exported frompackages/cli/src/define-config.tsand shipped throughpackages/cli/dist/define-config-*.d.ts.Root cause
packages/cli/src/define-config.ts:1148-1152— bothlazyPluginsoverloads are declared as returningArray<PluginOption> | undefined, and the implementation returnsundefinedwhenprocess.env[CONFIG_METADATA_ENV] === '1'(CONFIG_METADATA_ENVisVP_RESOLVING_CONFIG_METADATA,packages/cli/src/utils/constants.ts:145). The consumer side is Vite'sUserConfig(upstreamvite/packages/vite/src/node/config.ts:414), whose plugin field is optional without an explicitundefinedmember:plugins?: PluginOption[]. UnderexactOptionalPropertyTypes, an optional property may be omitted, but not assignedundefined— exactly the TS2345 observed.Under default
strict(withoutexactOptionalPropertyTypes),undefinedis assignable to optional properties, so the documented pattern compiles — which is why the existing type-compat tests inpackages/cli/src/__tests__/index.spec.tsnever caught it. Both the sync and async overloads have the problem.Why returning
[]is runtime-safeinjectPluginIntoInlineConfig(define-config.ts:894-912) already normalizes the field withconfig.plugins ?? []before injecting vite-plus' internal plugins, so[]andundefinedare indistinguishable there.plugins: []behaves the same asplugins: undefinedduring config load.crates/vp_migration/src/vite_config.rs:375+) and the static config extractor detectlazyPluginssyntactically; none depend on the runtimeundefinedreturn value.Edge cases
process.env.VP_RESOLVING_CONFIG_METADATA; no code in the repository branches on theundefinedreturn value.PluginOption[]is source-compatible for the documented usage, including the?? []workaround.Regression evidence
Not a regression. The
| undefinedreturn 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 enabledexactOptionalPropertyTypes, so the incompatibility was never covered.Confidence
high — the failing declaration and the optional-without-
undefinedtarget property are proven by code inspection, and the reproduction plus three controls isolate the cause exactly.Verification (bug vs intended)
docs/guide/troubleshooting.md:93-112presentsplugins: lazyPlugins(...)— sync and async — insidedefineConfigas the usage of the helper.vp migraterewrites inline Vite plugin arrays toplugins: lazyPlugins(() => [...]), so vite-plus' own tooling emits a config that does not type-check under this standardstrictflag combination.undefinedin the public type: theundefinedreturn 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 mentionsexactOptionalPropertyTypes.docs/guide/migrate-rules.md:208states vite-plus ownslazyPlugins.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,
bugverdict, vite-plus-owned code, no product decision needed, no assignees), so implementation proceeded.Changed files
packages/cli/src/define-config.ts— bothlazyPluginsoverloads (and the implementation signature) now returnArray<PluginOption>; the metadata-mode early return yields[]instead ofundefined, with a comment explaining why.packages/cli/src/__tests__/index.spec.ts— updated the four metadata-mode assertions fromtoBeUndefined()totoEqual([]); removed now-unneeded non-null assertions; initializedduring(= []) to satisfy definite-assignment analysis (TS2454) once the return type excludesundefined; new regression test — a compile-time assertion plus runtime check that the public return type never includesundefined:Under the old declaration this annotation resolves to
neverand fails to compile (TS2322: Type 'true' is not assignable to type 'never'); with the fix it istrue.Regression-test evidence (fails without the fix)
With the source change temporarily reverted and the spec change in place,
pnpm tsgofailed withTS2322: 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
pnpm -F vite-plus build-ts.d.tsnow declareslazyPlugins(...): Array<PluginOption>.exactOptionalPropertyTypes: true, sync)?? []exactOptionalPropertyTypespnpm tsgo(repo type check incl. spec files)docs/_data/team.ts, vitepress not installed), identical on the clean tree.vp test packages/cli/src/__tests__/index.spec.tsvp test packages/cli(full CLI unit suite)vp check(fmt + type-aware lint)docs/errors, identical count on the clean tree.VP_RESOLVING_CONFIG_METADATA=1→ returns[]for sync and async factories (factory not called); default mode returns the plugins array.Alternatives considered
defineConfig's accepted config type: rejected — loosens the public type contract for all users to paper over an internal sentinel.undefinedin the type but return[]at runtime: rejected — the public type would still break the documented usage, which is the reported bug.exactOptionalPropertyTypesrepo-wide: rejected as out of scope; the compile-time regression assertion covers the contract.Notes for release
The public return type narrows from
PluginOption[] | undefinedtoPluginOption[]. Source compatibility is preserved for documented usage (including the?? []workaround). Undocumented code that detected metadata mode by checking the return value forundefined(instead of readingVP_RESOLVING_CONFIG_METADATA) would observe[]; no such usage exists in the repository.vp migrateoutput anddocs/guide/troubleshooting.mdexamples are unchanged and now type-check underexactOptionalPropertyTypes.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