Skip to content

fix(types): improve defineConfig callback inference#21699

Closed
Vishnuuuu24 wants to merge 2 commits intovitejs:mainfrom
Vishnuuuu24:fix/defineconfig-callback-minify-21684
Closed

fix(types): improve defineConfig callback inference#21699
Vishnuuuu24 wants to merge 2 commits intovitejs:mainfrom
Vishnuuuu24:fix/defineconfig-callback-minify-21684

Conversation

@Vishnuuuu24
Copy link

Summary

  • improve defineConfig callback overload inference so sync/async callback configs preserve literal option types
  • fix callback config cases where literals like build.minify: 'terser' were widened and triggered TS2769
  • add dts regression tests for callback config minify literals without as const

Details

  • update defineConfig overloads in packages/vite/src/node/config.ts to use a callback-first generic signature:
    • defineConfig<T extends UserConfigFn>(config: T & UserConfigFn): ...
  • keep return typing behavior aligned with existing UserConfigFnObject / UserConfigFnPromise / UserConfigFn expectations
  • add tests in packages/vite/src/node/__tests_dts__/config.ts:
    • sync callback with build.minify: 'oxc'
    • async callback with build.minify: 'terser'
    • invalid nested build.unknown still errors (@ts-expect-error)

Validation

  • pnpm run build-types-check
  • pnpm run typecheck

Refs #21684

Copilot AI review requested due to automatic review settings February 24, 2026 10:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a TypeScript type inference issue where using defineConfig with async callbacks caused literal option types (like build.minify: 'terser') to be widened to string, resulting in TS2769 errors.

Changes:

  • Refactored defineConfig overloads to use a generic callback-first signature that preserves literal types
  • Added comprehensive dts regression tests for sync/async callbacks with minify literals

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/vite/src/node/config.ts Replaced three function overloads with a single generic overload that preserves literal types using conditional return types
packages/vite/src/node/tests_dts/config.ts Added three test cases validating literal type preservation in sync/async callbacks and invalid option detection

@sapphi-red sapphi-red closed this Feb 24, 2026
@Vishnuuuu24
Copy link
Author

Vishnuuuu24 commented Feb 24, 2026

Thanks for calling this out. You’re right, I used AI assistance while preparing this PR and parts ended up too close to #21698, including test shape and the proxy-bypass point you had already identified. I should have explicitly credited your prior work. Sorry about that, and thanks for the review.

@Vishnuuuu24
Copy link
Author

Vishnuuuu24 commented Feb 24, 2026 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants