Skip to content

feat: gracefully skip nullish values in storeToRefs - #3134

Merged
posva merged 2 commits into
vuejs:v4from
JSap0914:fix/storeToRefs-null-crash
Jun 19, 2026
Merged

posva merged 2 commits into
vuejs:v4from
JSap0914:fix/storeToRefs-null-crash

Conversation

@JSap0914

@JSap0914 JSap0914 commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Bug

storeToRefs throws TypeError: Cannot read properties of null (reading 'effect') when a setup store returns a non-ref null value as part of its state (closes #3123).

const useStore = defineStore('example', () => {
  return {
    // non-ref null is valid for optional state
    nullableItem: null as { id: number } | null,
    text: ref('hello'),
  }
})
// Before this fix: TypeError: Cannot read properties of null (reading 'effect')
const { text } = storeToRefs(useStore())

The loop in storeToRefs iterates all enumerable keys in the raw store. When a setup store returns a raw null value (not wrapped in ref()), rawStore[key] is literally null, and the unguarded value.effect access throws.

Fix

Add a null-guard before the .effect access:

- if (value.effect) {
+ if (value && value.effect) {

The guard is consistent with how isRef and isReactive already handle nullish values. A null (or undefined) raw value is simply skipped — the same outcome as any other non-ref, non-reactive value.

Verification

pnpm vitest run --project pinia packages/pinia/__tests__/storeToRefs.spec.ts
# → Tests 10 passed (10)

Red (before): test failed with TypeError: Cannot read properties of null (reading 'effect')
Green (after): all 10 storeToRefs tests pass, including the new regression test.

This PR was developed with AI assistance.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue where storeToRefs could throw when a setup store includes optional state values that are null (non-ref).
    • Updated detection logic to safely handle nullish values before reading internal properties.
  • Tests

    • Added coverage to ensure storeToRefs does not throw with null optional state and that existing refs are still returned and readable.

Copilot AI review requested due to automatic review settings June 18, 2026 17:44
@github-project-automation github-project-automation Bot moved this to 🆕 Triaging in Pinia Roadmap Jun 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 62517733-4139-4029-bb88-20428592211f

📥 Commits

Reviewing files that changed from the base of the PR and between 5b73b99 and d5bc446.

📒 Files selected for processing (1)
  • packages/pinia/src/storeToRefs.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/pinia/src/storeToRefs.ts

📝 Walkthrough

Walkthrough

storeToRefs is patched with a one-line null guard: value?.effect replaces value.effect using optional chaining to prevent a TypeError when a store state property holds null. A new test verifies storeToRefs does not throw for a setup store that returns a plain null alongside a ref.

Changes

storeToRefs null guard fix

Layer / File(s) Summary
Null guard and regression test
packages/pinia/src/storeToRefs.ts, packages/pinia/__tests__/storeToRefs.spec.ts
The computed-detection condition is tightened from value.effect to value?.effect using optional chaining, preventing a TypeError when state is null. A new test asserts storeToRefs does not throw for a setup store with a null optional property and still exposes the existing ref value.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Suggested reviewers

  • posva

Poem

🐇 A null crept in, the store fell down,
.effect was read and caused a frown.
Optional chaining — ?. — saved the day,
The refs returned, no TypeError's way.
Hop hop, the bunny patched it right! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately and clearly describes the primary change: fixing a null guard in storeToRefs that prevents a TypeError when setup stores return non-ref null values.
Linked Issues check ✅ Passed The code changes directly address the requirements from issue #3123 by adding a null guard to prevent TypeError when accessing value.effect on null values.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the null guard bug in storeToRefs and adding a regression test; no unrelated modifications are present.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/pinia/__tests__/storeToRefs.spec.ts (1)

215-217: ⚡ Quick win

Strengthen the regression by asserting the null non-ref key is excluded

This test proves the crash is fixed, but it should also lock in expected behavior that nullableItem is skipped in setup-store refs.

Suggested test tightening
-    expect(() => storeToRefs(store)).not.toThrow()
-    const { text } = storeToRefs(store)
-    expect(text.value).toBe('hello')
+    expect(() => storeToRefs(store)).not.toThrow()
+    const refs = storeToRefs(store)
+    expect(refs).not.toHaveProperty('nullableItem')
+    expect(refs.text.value).toBe('hello')
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/pinia/__tests__/storeToRefs.spec.ts` around lines 215 - 217, The
test currently verifies that storeToRefs does not throw and checks the text
property, but it does not assert that the nullableItem key (which is null) is
excluded from the refs returned. Add an additional assertion after obtaining the
refs from storeToRefs to verify that nullableItem is undefined or not present in
the destructured refs, ensuring that null non-ref keys are properly skipped in
setup-store refs and strengthening the regression test.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/pinia/__tests__/storeToRefs.spec.ts`:
- Around line 215-217: The test currently verifies that storeToRefs does not
throw and checks the text property, but it does not assert that the nullableItem
key (which is null) is excluded from the refs returned. Add an additional
assertion after obtaining the refs from storeToRefs to verify that nullableItem
is undefined or not present in the destructured refs, ensuring that null non-ref
keys are properly skipped in setup-store refs and strengthening the regression
test.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c9d1248f-83ac-4c28-8c55-9968ff4d11a2

📥 Commits

Reviewing files that changed from the base of the PR and between f352243 and 5b73b99.

📒 Files selected for processing (2)
  • packages/pinia/__tests__/storeToRefs.spec.ts
  • packages/pinia/src/storeToRefs.ts

Comment thread packages/pinia/src/storeToRefs.ts Resolved

@posva posva left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! This is actually not valid but the runtime change is worth having.

@posva posva changed the title fix: guard against null in storeToRefs when setup store returns non-ref null feat: gracefully skip nullish values in storeToRefs Jun 19, 2026
@posva
posva merged commit cac8263 into vuejs:v4 Jun 19, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from 🆕 Triaging to ✅ Done in Pinia Roadmap Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

storeToRefs throws TypeError: Cannot read properties of null (reading 'effect') when store state is null (Vue 3.5)

3 participants