fix(types): support union rel/type in defineLink and defineScript#765
Conversation
📝 WalkthroughWalkthroughPR enhances ChangesUnion-aware type inference for link and script
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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
|
Allows runtime-determined rels like `cond ? 'preconnect' : 'dns-prefetch'` and script types like `cond ? 'module' : 'text/javascript'` without casts. Distributes the match over the input union, then intersects matching variants (minus the discriminator) so optional fields from any branch carry over while incompatible required fields still force the stricter shape.
b0ea905 to
03fd84f
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/unhead/src/types/schema/script.ts (1)
485-488: 💤 Low valueConsider extracting shared helper types to
util.ts.
UnionToIntersectionandIsUnionare duplicated verbatim in bothlink.tsandscript.ts. Moving them to../util.tswould eliminate duplication and centralize these type utilities.🤖 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/unhead/src/types/schema/script.ts` around lines 485 - 488, Extract the duplicated type helpers UnionToIntersection and IsUnion from script.ts and link.ts into a single ../util.ts file, export them there, then remove the duplicate definitions in both script.ts and link.ts and add imports (e.g., import { UnionToIntersection, IsUnion } from "../util") where those types are used; ensure exported names and usages (type UnionToIntersection<...>, type IsUnion<...>) remain identical to avoid breaking consumers.
🤖 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/unhead/src/types/schema/script.ts`:
- Around line 485-488: Extract the duplicated type helpers UnionToIntersection
and IsUnion from script.ts and link.ts into a single ../util.ts file, export
them there, then remove the duplicate definitions in both script.ts and link.ts
and add imports (e.g., import { UnionToIntersection, IsUnion } from "../util")
where those types are used; ensure exported names and usages (type
UnionToIntersection<...>, type IsUnion<...>) remain identical to avoid breaking
consumers.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 2d535838-0fb7-451e-8f89-5b5f615b733d
📒 Files selected for processing (3)
packages/unhead/src/types/schema/link.tspackages/unhead/src/types/schema/script.tspackages/unhead/test/unit/types.test.ts
🔗 Linked issue
N/A
❓ Type of change
📚 Description
defineLink/defineScriptpreviously rejected runtime-determined rels and types (e.g.cond ? 'preconnect' : 'dns-prefetch') becauseInferLink/InferScriptonly matched single-literal inputs against the discriminated union.Distributes the match over the input union, then intersects matching variants (minus their discriminator) so optional fields like
crossorigincarry over while incompatible required fields (e.g.preload'sas,application/ld+json'stextContent) still force the stricter shape. Single-literal inputs remain strictly narrowed.Adversarial type-test coverage added for resource-hint unions, FaviconLink's internal rel union, generic-rel fallthrough, and script-type unions.
Summary by CodeRabbit
relandtypevalues, with enhanced IDE support and field validation.