Skip to content

feat: add sanity client interface type - #1063

Draft
stipsan wants to merge 13 commits into
mainfrom
add-sanity-client-interface-type
Draft

stipsan wants to merge 13 commits into
mainfrom
add-sanity-client-interface-type

Conversation

@stipsan

@stipsan stipsan commented May 2, 2025 •

Copy link
Copy Markdown
Member

The new SanityClientType interface is to solve problems we often see with SanityClient and duplicate instances of @sanity/client in node_modules:

next-app-router:build: ./app/variants/live-store/PreviewProvider.tsx:18:24
next-app-router:build: Type error: Type 'SanityClient' is not assignable to type 'SanityClient'.
next-app-router:build:   Property '#private' in type 'SanityClient' refers to a different member that cannot be accessed from within type 'SanityClient'.
next-app-router:build: 
next-app-router:build:   16 |   if (!token) throw new TypeError('Missing token')
next-app-router:build:   17 |   return (
next-app-router:build: > 18 |     <LiveQueryProvider client={client} token={token} logger={console} perspective={perspective}>
next-app-router:build:      |                        ^
next-app-router:build:   19 |       {children}
next-app-router:build:   20 |     </LiveQueryProvider>
next-app-router:build:   21 |   )
Next.js build worker exited with code: 1 and signal: null

These errors are confusing and frustrating. The reason why our generated SanityClient class types ends up with the #private marker is due to us using private properties to ensure implementation details like #httpRequest can't be accessed on client.httpRequest.
There isn't a way for us to use the private property while also not ending up with the strict and brittle typing for SanityClient.

The solution I've landed on is to expose a new interface, SanityClientType, that the SanityClient class has to implement.
This works well for type checking, as the #private property is no longer there, and the union logic TypeScript is known and loved for is used, making the client no longer so sensitive to duplicates in node_modules.

@vercel

vercel Bot commented May 2, 2025 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
tsdocs-client ⬜️ Ignored (Inspect) May 2, 2025 11:52am

@github-actions

github-actions Bot commented May 2, 2025 •

Copy link
Copy Markdown
Contributor

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 87.67% 2703 / 3083
🔵 Statements 87.67% 2703 / 3083
🔵 Functions 87.02% 228 / 262
🔵 Branches 88.7% 880 / 992
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/SanityClient.ts 71.68% 97.67% 58.97% 71.68% 110-111, 142-153, 214-226, 235-239, 251-255, 313-324, 382-393, 451-462, 569-580, 638-649, 686-687, 695-698, 707-711, 729-730, 739-740, 1417-1418
src/assets/AssetsClient.ts 94.16% 85.71% 100% 94.16% 249-252, 287, 300-301
src/datasets/DatasetsClient.ts 84.72% 100% 69.23% 84.72% 86-87, 96-97, 105-106, 112-116
src/projects/ProjectsClient.ts 80% 100% 75% 80% 69-73, 81-83
src/users/UsersClient.ts 77.77% 100% 83.33% 77.77% 44-51
Generated in workflow #3204 for commit 9be0a0e by the Vitest Coverage Report Action

This branch has not been deployed

No deployments
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.

1 participant