Skip to content

feat: implement TypeGen support#210

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/implement-typegen-support
Draft

feat: implement TypeGen support#210
Copilot wants to merge 2 commits into
mainfrom
copilot/implement-typegen-support

Conversation

Copilot AI commented May 6, 2026

Copy link
Copy Markdown
Contributor

Adds full Sanity TypeGen support, mirroring the approach from @portabletext/react (PR #309). Purely type-level — zero runtime changes.

Changes

  • Generic component interfacesPortableTextHtmlComponents<B>, PortableTextComponents<B>, and PortableTextOptions<B> now flow B from the value parameter through to component handlers
  • Type inference machinery — 3-branch conditional component map types that gracefully degrade: precise literals → narrowed handlers, wide/unknown → loose Record<string, ...>, no custom members → fallback
  • New public utility types:
    • InferComponents<T> — forgiving component map inferred from value type (extra handlers allowed)
    • InferStrictComponents<T> — strict mode requiring all custom handlers, rejecting unknown ones
    • InferValue<T> — recursively extracts PT array item types from any TypeGen query result
    • DefaultPortableTextBlockStyle, DefaultPortableTextListItem, DefaultPortableTextMark
  • Type-level tests covering inference for custom types, marks, blocks, lists, strict mode, and InferValue

Usage

import {toHTML} from '@portabletext/to-html'
import type {InferComponents} from '@portabletext/to-html'

// Components are now type-aware when value type is known
const components = {
  types: {
    // `value` is narrowed to ImageBlock
    image: ({value}) => `<img src="${value.url}" alt="${value.alt}" />`,
    code: ({value}) => `<pre><code>${value.code}</code></pre>`,
  },
} satisfies InferComponents<typeof data.content>

toHTML(data.content, {components})

Backward compatible — all generics default to any, so existing untyped usage is unchanged.

@changeset-bot

changeset-bot Bot commented May 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a4e1a37

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

This PR includes changesets to release 1 package
Name Type
@portabletext/to-html Minor

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

…onents, and InferValue utility types

Agent-Logs-Url: https://github.com/portabletext/to-html/sessions/1cd83452-eb66-48b4-a3a4-55c9955100c5

Co-authored-by: stipsan <81981+stipsan@users.noreply.github.com>
@stipsan stipsan force-pushed the copilot/implement-typegen-support branch from 0e05082 to 5b2c709 Compare May 6, 2026 12:38

@stipsan stipsan 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.

@copilot no no, I want you to setup the same exact testing suite https://github.com/portabletext/react-portabletext/tree/d15decca09351dcef693373746d31bacf052d3dd/test/typegen where the only difference is that instead of assertions with react like:
;<PortableText value={bio} components={components} />
you will be doing
toHTML(bio, {components})

Also, the release notes in your changeset/typegen-support.md should be exactly like https://github.com/portabletext/react-portabletext/blob/e59cacb5db777381517a19307512fdb12ce61c9e/.changeset/typegen-component-inference.md

Copilot AI commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

@copilot no no, I want you to setup the same exact testing suite https://github.com/portabletext/react-portabletext/tree/d15decca09351dcef693373746d31bacf052d3dd/test/typegen where the only difference...

Done in a4e1a37. Set up the same exact test suite from test/typegen/ in react-portabletext, with all supporting files (sanity.cli.ts, sanity.config.ts, sanity.types.ts, schema.json). The typegen.test-d.ts mirrors the React test file but uses toHTML(content ?? [], {components}) instead of <PortableText value={content} components={components} />. The changeset now matches the react-portabletext format. All 73 type tests pass.

@socket-security

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​sanity/​client@​7.22.07810010096100
Addedgroq@​5.24.010010080100100

View full report

Copilot AI requested a review from stipsan May 6, 2026 12:58
Copilot finished work on behalf of stipsan May 6, 2026 12:58
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