Skip to content

feat(context) [next] - Improve createContextProvider types, Add createOptionalContextProvider - #923

Merged
atk merged 2 commits into
solidjs-community:nextfrom
Monkeylordz:context
May 31, 2026
Merged

feat(context) [next] - Improve createContextProvider types, Add createOptionalContextProvider#923
atk merged 2 commits into
solidjs-community:nextfrom
Monkeylordz:context

Conversation

@Monkeylordz

@MonkeylordzMonkeylordz commented May 31, 2026

Copy link
Copy Markdown

In Solid 2.0, useContext now throws if the value is undefined, which made the existing createContextProvider types out-of-date. Is also introduced a gap which we can solve - namely that some contexts may actually want a missing or undefined value. This PR makes the following additions to address these issues:

  • Changed createContextProvider's types so that the useContext function always returns a non-undefined value
  • Added createOptionalContextProvider which allows undefined/missing values

Summary by CodeRabbit

  • New Features

    • Introduced createOptionalContextProvider to handle missing or undefined context values with optional fallback support.
    • Enhanced createContextProvider with stronger type safety guarantees for required context scenarios.
  • Documentation

    • Expanded context API documentation with comprehensive usage examples and detailed behavior specifications for both providers.

…eturn a non-undefined value
- Added `createOptionalContextProvider` that allows undefined/missing values
@changeset-bot

changeset-botBot commented May 31, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5c17bab

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
@solid-primitives/contextMinor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitaiBot commented May 31, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 0b84d09d-08d9-4210-9e55-c908733be926

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

This PR updates the @solid-primitives/context package to align with Solid 2 context semantics. It adds createOptionalContextProvider for safe handling of missing context, updates createContextProvider to exclude undefined from its return type, and provides comprehensive documentation and test coverage.

Changes

Context API Enhancement

Layer / File(s)Summary
Core implementation: sentinel and provider functions
packages/context/src/index.ts
Adds $EMPTY sentinel symbol and updates createContextProvider to use Exclude<T, undefined> for the useContext return type; introduces new createOptionalContextProvider that returns `T
Documentation and package metadata
packages/context/README.md, .changeset/context-solid2-context-api.md, packages/context/package.json
Expands README with separate sections documenting both provider APIs, clarifies that createContextProvider's useContext excludes undefined and throws on missing context, explains createOptionalContextProvider's fallback and undefined-returning behavior, adds changeset entry, and registers the new primitive in package metadata.
Test coverage for both provider APIs
packages/context/test/index.test.tsx
Imports and verifies both functions with expanded test cases: validates that createContextProvider throws when no provider or fallback exists and types correctly, verifies createOptionalContextProvider returns undefined for missing providers or undefined values, confirms type-level return type assertions, and applies minor formatting adjustments to existing tests.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • solidjs-community/solid-primitives#907: Modifies createContextProvider type handling and adds createStrictContextProvider/createLayeredContext with overlapping changes to context provider semantics.

Suggested labels

solid-2.0

Poem

🐰 Two paths through context trees we weave,
One throws when values we don't receive,
The other kindly returns undefined—
A sentinel dance, by Solid's design!
Optional safety, required clarity bright,
Hop along freely, context feels right.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check nameStatusExplanation
Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
Title check✅ PassedThe title accurately reflects the main changes: improving createContextProvider types and adding createOptionalContextProvider, with clear versioning context via [next].
Docstring Coverage✅ PassedDocstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.

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

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

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

@Monkeylordz

Copy link
Copy Markdown
Author

@CodeRabbit review

@coderabbitai

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

atk
atk approved these changes May 31, 2026
@atk
atk merged commit 42c92e7 into solidjs-community:nextMay 31, 2026
1 check passed
Sign up for freeto 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

@Monkeylordz@atk@davedbase