Skip to content

fix(types): accept readonly and as const inputs#736

Merged
harlan-zw merged 5 commits into
mainfrom
fix/readonly-type-narrowing
Apr 10, 2026
Merged

fix(types): accept readonly and as const inputs#736
harlan-zw merged 5 commits into
mainfrom
fix/readonly-type-narrowing

Conversation

@harlan-zw

@harlan-zw harlan-zw commented Apr 10, 2026

Copy link
Copy Markdown
Collaborator

🔗 Linked issue

N/A

❓ Type of change

  • 📖 Documentation
  • 🐞 Bug fix
  • 👌 Enhancement
  • ✨ New feature
  • 🧹 Chore
  • ⚠️ Breaking change

📚 Description

Schema struct types (SpeculationRules, MetaFlat) used mutable arrays (string[]) which rejected as const literals. Changed all user-facing arrays to readonly so both mutable and readonly inputs are accepted without wrappers.

Added DeepReadonly utility type applied to InferScript/InferLink return types so defineScript/defineLink also handle readonly generics correctly.

Re-exported defineLink/defineScript from all framework packages (@unhead/vue, @unhead/react, @unhead/solid-js, @unhead/svelte, @unhead/angular).

Fixed UseScriptOptions TS 6 compatibility where Pick<> on optional properties needs Partial<> wrapper to preserve optionality.

Summary by CodeRabbit

  • New Features

    • defineLink and defineScript utilities are now exposed in Angular, React, Solid.js, Svelte, and Vue packages.
  • Improvements

    • Type system tightened to make link, script, metadata, and speculation-rules shapes deeply readonly for stronger immutability guarantees.
  • Tests

    • Added unit tests covering script and link patterns, including structured and readonly usages.

Schema struct types (SpeculationRules, MetaFlat) used mutable arrays
which rejected `as const` literals. Changed all user-facing arrays to
`readonly` so both mutable and readonly inputs are accepted.

Added DeepReadonly utility applied to InferScript/InferLink return
types so defineScript/defineLink also handle readonly generics.

Re-exported defineLink/defineScript from all framework packages
(@unhead/vue, @unhead/react, @unhead/solid-js, @unhead/svelte,
@unhead/angular).
Pick<> on optional properties needs Partial<> wrapper in TS 6 to
preserve optionality.
@coderabbitai

coderabbitai Bot commented Apr 10, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@harlan-zw has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 2 minutes and 44 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 2 minutes and 44 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9de20271-41fb-4a65-8dc5-9d0512b74f9f

📥 Commits

Reviewing files that changed from the base of the PR and between 8eb3f29 and 28a8e43.

📒 Files selected for processing (1)
  • packages/unhead/src/types/util.ts
📝 Walkthrough

Walkthrough

Adds re-exports defineLink and defineScript to multiple framework packages; introduces DeepReadonly<T>; converts several array types to readonly; makes inferred Link/Script types deeply readonly; relaxes some Vue UseScriptOptions; and adds tests and export manifest updates.

Changes

Cohort / File(s) Summary
Framework Public API Exports
packages/angular/src/public-api.ts, packages/react/src/index.ts, packages/solid-js/src/index.ts, packages/svelte/src/index.ts, packages/vue/src/index.ts
Re-export defineLink and defineScript from unhead in each framework package public API.
Type System Utilities
packages/unhead/src/types/util.ts
Add exported DeepReadonly<T> generic type that recursively makes objects/arrays readonly while preserving functions/primitives.
Schema Type Inference
packages/unhead/src/types/schema/link.ts, packages/unhead/src/types/schema/script.ts
Wrap resolved/inferred link and script shapes in DeepReadonly<>, changing inference results to deeply readonly types.
Schema Array Constraints
packages/unhead/src/types/schema/metaFlat.ts, packages/unhead/src/types/schema/struct/speculationRules.ts
Convert several mutable array properties to readonly arrays (article/book author/tag, speculation rules prefetch/prerender/requires/urls, and nested rule arrays).
Vue Script Options
packages/vue/src/scripts/useScript.ts
Make use, eventContext, and beforeInit optional in UseScriptOptions via Partial<Pick<...>>.
Tests / Export Manifests
packages/unhead/test/unit/define.test.ts, test/exports/react.yaml, test/exports/solid-js.yaml, test/exports/svelte.yaml, test/exports/vue.yaml
Add/extend tests for defineScript/structured script payloads and as const patterns; update YAML export manifests to include defineLink and defineScript.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Poem

🐰 I nibble types both firm and sweet,

I tuck readonly where arrays meet,
I hop across frameworks, near and far,
Re-exporting helpers like a little star,
A rabbit's clap for tidy code complete.

🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 passed)
Check name Status Explanation
Description check ✅ Passed The description covers all required sections: linked issue, type of change (bug fix selected), and detailed explanation of the changes made.
Title check ✅ Passed The title 'fix(types): accept readonly and as const inputs' accurately summarizes the main change—updating TypeScript typings to accept readonly and as const inputs.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/readonly-type-narrowing

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.

@github-actions

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Bundle Size Gzipped
Client (Minimal) 10.7 kB 4.4 kB
Server (Minimal) 10.6 kB 4.3 kB
Vue Client (Minimal) 11.8 kB 4.8 kB
Vue Server (Minimal) 11.6 kB 4.7 kB

@harlan-zw harlan-zw requested a review from Copilot April 10, 2026 05:54
@harlan-zw harlan-zw changed the title fix: accept readonly and as const inputs in type narrowing fix(types): accept readonly and as const inputs Apr 10, 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.

Pull request overview

This PR adjusts Unhead’s TypeScript types to better accept readonly / as const inputs during type narrowing (notably for scripts, links, metadata, and speculation rules), and re-exports defineLink/defineScript from framework wrapper packages.

Changes:

  • Make several schema “array” fields readonly to accept as const literals without casts/wrappers.
  • Introduce DeepReadonly and apply it to InferScript / InferLink to improve readonly-friendly narrowing.
  • Re-export defineLink / defineScript from framework packages and extend unit tests/export snapshots accordingly.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/exports/vue.yaml Updates Vue package export snapshot to include defineLink/defineScript.
test/exports/svelte.yaml Updates Svelte package export snapshot to include defineLink/defineScript.
test/exports/solid-js.yaml Updates Solid package export snapshot to include defineLink/defineScript.
test/exports/react.yaml Updates React package export snapshot to include defineLink/defineScript.
packages/vue/src/scripts/useScript.ts Adjusts UseScriptOptions typing for TS 6 optionality behavior.
packages/vue/src/index.ts Re-exports defineLink/defineScript from unhead.
packages/unhead/test/unit/define.test.ts Adds compile-time/runtime coverage for readonly/as-const link/script patterns.
packages/unhead/src/types/util.ts Adds DeepReadonly utility type for readonly-friendly inference.
packages/unhead/src/types/schema/struct/speculationRules.ts Switches speculation rules arrays to readonly and updates related types.
packages/unhead/src/types/schema/script.ts Applies DeepReadonly to InferScript results.
packages/unhead/src/types/schema/metaFlat.ts Switches MetaFlat array fields to readonly.
packages/unhead/src/types/schema/link.ts Applies DeepReadonly to InferLink results.
packages/svelte/src/index.ts Re-exports defineLink/defineScript from unhead.
packages/solid-js/src/index.ts Re-exports defineLink/defineScript from unhead.
packages/react/src/index.ts Re-exports defineLink/defineScript from unhead.
packages/angular/src/public-api.ts Re-exports defineLink/defineScript from unhead.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/unhead/src/types/util.ts Outdated
Comment on lines +31 to +32
* Used in type narrowing so that `defineScript`/`defineLink` accept both
* mutable and `as const` (readonly) inputs without touching schema types.
@harlan-zw harlan-zw merged commit 5ccf98e into main Apr 10, 2026
8 checks passed
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.

2 participants