fix(types): accept readonly and as const inputs#736
Conversation
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.
|
Warning Rate limit exceeded
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 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 configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAdds re-exports Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Bundle Size Analysis
|
There was a problem hiding this comment.
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
readonlyto acceptas constliterals without casts/wrappers. - Introduce
DeepReadonlyand apply it toInferScript/InferLinkto improve readonly-friendly narrowing. - Re-export
defineLink/defineScriptfrom 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.
| * Used in type narrowing so that `defineScript`/`defineLink` accept both | ||
| * mutable and `as const` (readonly) inputs without touching schema types. |
🔗 Linked issue
N/A
❓ Type of change
📚 Description
Schema struct types (
SpeculationRules,MetaFlat) used mutable arrays (string[]) which rejectedas constliterals. Changed all user-facing arrays toreadonlyso both mutable and readonly inputs are accepted without wrappers.Added
DeepReadonlyutility type applied toInferScript/InferLinkreturn types sodefineScript/defineLinkalso handle readonly generics correctly.Re-exported
defineLink/defineScriptfrom all framework packages (@unhead/vue,@unhead/react,@unhead/solid-js,@unhead/svelte,@unhead/angular).Fixed
UseScriptOptionsTS 6 compatibility wherePick<>on optional properties needsPartial<>wrapper to preserve optionality.Summary by CodeRabbit
New Features
Improvements
Tests