Skip to content

fix(types): validate defineModel defaults#14968

Open
ifer47 wants to merge 2 commits into
vuejs:mainfrom
ifer47:fix/define-model-default-types
Open

fix(types): validate defineModel defaults#14968
ifer47 wants to merge 2 commits into
vuejs:mainfrom
ifer47:fix/define-model-default-types

Conversation

@ifer47

@ifer47 ifer47 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Closes #14966

Summary

  • Reject non-factory object / array defaults for \defineModel\ the same way \withDefaults\ does.\n- Keep scalar defaults and factory defaults working as before.\n\n## Verification\n- pnpm test-dts-only

Summary by CodeRabbit

  • Improvements
    • Enhanced TypeScript type checking for model definitions to enforce factory functions for arrays and objects as defaults, preventing common mistakes and providing clearer error messages.

@github-actions

Copy link
Copy Markdown

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 106 kB 40.2 kB 36.1 kB
vue.global.prod.js 164 kB 60.2 kB 53.5 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 48.8 kB 19 kB 17.4 kB
createApp 56.9 kB 22 kB 20.1 kB
createSSRApp 61.2 kB 23.8 kB 21.7 kB
defineCustomElement 63.1 kB 23.9 kB 21.8 kB
overall 71.7 kB 27.4 kB 25 kB

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

apiSetupHelpers.ts introduces two internal helper types—DefineModelRuntimeOptions and DefineModelDefault—that replace the unconstrained { default: any } in all four defineModel overloads with { default: DefineModelDefault<T> }, using InferDefault<Data, T> to require factory functions for array/object types. Type tests are extended to verify both the accepted factory form and the rejected raw-literal form.

Changes

defineModel default factory type enforcement

Layer / File(s) Summary
Helper types and overload signatures
packages/runtime-core/src/apiSetupHelpers.ts
Adds type Data import; introduces DefineModelRuntimeOptions<T,G,S> (built from PropOptions<T> with default omitted, plus model get/set fields) and DefineModelDefault<T> (via InferDefault<Data,T>); updates all four named/unnamed defineModel overloads to use these types instead of { default: any }.
Type tests
packages-private/dts-test/setupHelpers.test-d.ts
Adds positive expectType assertions for defineModel<number[]> and defineModel<{ foo: string }> with factory defaults, and adds @ts-expect-error assertions confirming raw array/object literals are rejected as default.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

scope: types, :hammer: p3-minor-bug

Suggested reviewers

  • edison1105
  • KazariEX
  • johnsoncodehk

Poem

🐇 No bare arrays slipping through the gate,
A factory function seals each default's fate.
InferDefault watches, strict and true,
DefineModelDefault enforces the rule.
Hop hop — consistent types, a bug laid low! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(types): validate defineModel defaults' accurately describes the main change: adding type validation for defineModel default values to prevent invalid non-factory object/array defaults.
Linked Issues check ✅ Passed The PR fully addresses issue #14966 by implementing type validation to reject non-factory object/array defaults in defineModel, matching withDefaults behavior and preventing shared mutable state.
Out of Scope Changes check ✅ Passed All changes are strictly within scope: test assertions for defineModel typing and type signature updates in apiSetupHelpers.ts directly implement the validation requirement from issue #14966.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ifer47

ifer47 commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

Tests are green. The only failing check is continuous-release, and it fails inside pkg-pr-new publish with:\n\n404: There is no workflow defined for Mto6kyTnt7\n\nI compared this with other PRs from the same fork, and the same check passed there, so this looks like an external pkg.pr.new/service issue rather than a regression in this change. A rerun should be enough once the service is healthy.

@pkg-pr-new

pkg-pr-new Bot commented Jun 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@14968
npm i https://pkg.pr.new/@vue/compiler-core@14968
yarn add https://pkg.pr.new/@vue/compiler-core@14968.tgz

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@14968
npm i https://pkg.pr.new/@vue/compiler-dom@14968
yarn add https://pkg.pr.new/@vue/compiler-dom@14968.tgz

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@14968
npm i https://pkg.pr.new/@vue/compiler-sfc@14968
yarn add https://pkg.pr.new/@vue/compiler-sfc@14968.tgz

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@14968
npm i https://pkg.pr.new/@vue/compiler-ssr@14968
yarn add https://pkg.pr.new/@vue/compiler-ssr@14968.tgz

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@14968
npm i https://pkg.pr.new/@vue/reactivity@14968
yarn add https://pkg.pr.new/@vue/reactivity@14968.tgz

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@14968
npm i https://pkg.pr.new/@vue/runtime-core@14968
yarn add https://pkg.pr.new/@vue/runtime-core@14968.tgz

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@14968
npm i https://pkg.pr.new/@vue/runtime-dom@14968
yarn add https://pkg.pr.new/@vue/runtime-dom@14968.tgz

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@14968
npm i https://pkg.pr.new/@vue/server-renderer@14968
yarn add https://pkg.pr.new/@vue/server-renderer@14968.tgz

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@14968
npm i https://pkg.pr.new/@vue/shared@14968
yarn add https://pkg.pr.new/@vue/shared@14968.tgz

vue

pnpm add https://pkg.pr.new/vue@14968
npm i https://pkg.pr.new/vue@14968
yarn add https://pkg.pr.new/vue@14968.tgz

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@14968
npm i https://pkg.pr.new/@vue/compat@14968
yarn add https://pkg.pr.new/@vue/compat@14968.tgz

commit: 6325b6e

@edison1105

Copy link
Copy Markdown
Member

/ecosystem-ci run

@edison1105 edison1105 added the ready to merge The PR is ready to be merged. label Jun 17, 2026
@vue-bot

vue-bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

📝 Ran ecosystem CI: Open

suite result latest scheduled
quasar success success
nuxt success success
vant success success
vue-simple-compiler success success
language-tools success success
router success success
vite-plugin-vue success success
vue-i18n success success
test-utils success success
vitepress success success
vuetify success success
vue-macros success success
pinia success success
radix-vue failure failure
vueuse success success
primevue success success

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

Labels

ready to merge The PR is ready to be merged. scope: types

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistency in the validation of defaults between withDefaults and defineModel

3 participants