-
Notifications
You must be signed in to change notification settings - Fork 431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: resolve type error by inlining DocumentComponents definition #6703
Conversation
- Addressed type error with 'preview' property in 'DocumentComponents' - Inlined type definitions within module augmentation to avoid naming collision - Ensured build process prefers correct type for '.d.ts' files - Used type import to prevent runtime impact
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Ignored Deployment
|
No changes to documentation |
Component Testing Report Updated May 16, 2024 6:41 PM (UTC)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, I wasn't aware we were using declare module '@sanity/types' {
type techniques in the build of sanity
🤔
When it comes to generating .d.ts
builds it's unfortunately the case that you often can accidentally overwrite exports silently, like the DocumentComponents
case here.
In some cases you'll get a warning if exports collide, if they happen in an ambiguous way. In most cases it's viewed as intentional by rollup and the TypeScript compiler.
The way the rollup .d.ts
build works is that we:
- First run
tsc
to generate.d.ts
files in a temporary directory, as there is no other build tool that can generate definition files as well as tsc can. - Since
tsc
isn't a bundler, you'll get oned.ts
file for every.tsx
and.ts
in yoursrc
folder. We use these files as input with@microsoft/api-extractor
. @microsoft/api-extractor
doesn't support multiple entrypoints for a single npm package, so we run it once per entry (forsanity
it means 16 times, once perexports
inpackage.json
, and once perbundles
inpackage.config.ts
), and it provides us with the neat, one.d.ts
file per entry.
It's in step 3 that the overwrite happens and there isn't much we can do about it from the @sanity/pkg-utils
.
The best guard I've seen is to use a setup like vitest' typecheck where you ensure that generated types on public interfaces work as they should and that you're not missing exports or doing unintentional overrides.
Description
This PR addresses a type error caused by the missing 'preview' property in 'DocumentComponents'. The error, "Object literal may only specify known properties, and 'preview' does not exist in type 'DocumentComponents'", was resolved by inlining the type definitions within the module augmentation. This ensures TypeScript recognizes the 'preview' property as part of 'DocumentComponents' and avoids naming collisions that were causing the build process to prefer the incorrect type.
Note that this should not change the public API of the package at all due to the re-export but I may be missing something. An alternative would be to rename the
DocumentComponents
interface to something that doesn't collide.Closes #6542
The fix involves:
.d.ts
files.This change was necessary to maintain consistency across the codebase and to resolve the type error encountered during the build process.
What to review
DocumentComponents
are correctly inlined within the module augmentation..d.ts
files are correctly generated.Added @stipsan as a reviewer to verify this has no affect on the build and to give insight on how the build outputs declaration files.
Testing
I manually tested the result by running the build and copying the resulting
.d.ts
files into a fresh project that previously showed the issue. The type error was gone, confirming that the fix works as intended.Notes for release
Resolves a type error caused by the missing 'preview' property in 'DocumentComponents'.