-
-
Notifications
You must be signed in to change notification settings - Fork 523
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 TypeScript schema error when using project references #1777
Fix TypeScript schema error when using project references #1777
Conversation
…project references
🦋 Changeset detectedLatest commit: 702b83a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@@ -117,7 +117,9 @@ type BaseSchemaWithoutEffects = | |||
type BaseSchema = BaseSchemaWithoutEffects | z.ZodEffects<BaseSchemaWithoutEffects>; | |||
|
|||
/** Type that extends Starlight’s default schema with an optional, user-defined schema. */ | |||
type ExtendedSchema<T extends BaseSchema> = T extends BaseSchema | |||
type ExtendedSchema<T extends BaseSchema | never = never> = [T] extends [never] |
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.
This pattern is used to disable distributivity as when conditional types act on a generic type, they become distributive when given a union type which can be the case here.
Marking this as ready for review now as I did not find so far anything breaking with the change so happy to get people trying it out and getting feedback. |
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.
I have only a hazy understanding of this issue, but fully trust you and the code change seems fine to me!
Needs a changeset though — hopefully if we can describe what this fixes simply, it won’t be as complex as the concept of distributivity 😁
* main: Update fa.json (withastro#1793) i18n(fr): update `resources/community-content.mdx` (withastro#1795) [ci] format i18n(ko-KR): update `community-content.mdx` (withastro#1794) [ci] format Adds Starlight in Next.js project video (withastro#1789) [ci] format i18n(ru): update translations (withastro#1783) i18n(id): Update getting-started.mdx (withastro#1778) Upgrade Lunaria and add Action (withastro#1768) [ci] format i18n(es): update `sidebar` (withastro#1767) i18n(pt-br): Update `getting-started.mdx` (withastro#1776) i18n(es): update `configuration` (withastro#1766) [ci] format Add TrueCharts to showcases (withastro#1773)
Took a stab at this, hope this makes any sense 😆 |
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.
Changeset looks good to me! No docs in this one, so I think we can merge right away. Thanks @HiDeoo 🎉
* main: (63 commits) Add translation for copy button (withastro#1788) Update astro-expressive-code (withastro#1720) Fix type inference for extended i18n schemas (withastro#1786) Fix TypeScript schema error when using project references (withastro#1777) Update fa.json (withastro#1793) i18n(fr): update `resources/community-content.mdx` (withastro#1795) [ci] format i18n(ko-KR): update `community-content.mdx` (withastro#1794) [ci] format Adds Starlight in Next.js project video (withastro#1789) [ci] format i18n(ru): update translations (withastro#1783) i18n(id): Update getting-started.mdx (withastro#1778) Upgrade Lunaria and add Action (withastro#1768) [ci] format i18n(es): update `sidebar` (withastro#1767) i18n(pt-br): Update `getting-started.mdx` (withastro#1776) i18n(es): update `configuration` (withastro#1766) [ci] format Add TrueCharts to showcases (withastro#1773) ...
What kind of changes does this PR include?
Description
declarations: true
#1612The issue in #1612 is related to having
composite
set totrue
in a usertsconfig.json
. This option, common for projects using project references, also enables thedeclaration
option, which would generate a TypeScript error in thesrc/content/config.ts
file:Even tho a workaround could be to just disable
composite
specifically in the Starlight project, this could prevent even type validation usingtsc
.The issue is related to the recursion happening in the
BaseSchemaWithoutEffects
type of the schema.After spending many hours playing with
tsc --extendedDiagnostics
,tsc --generateTrace
,chrome://tracing
,Perfetto
, and@typescript/analyze-trace
, I did not find a way to optimize the type to avoid the error while still keeping the exact same behavior and possibilities it offers.Fortunately, and surprisingly, I noticed that the issue only happens when the
docsSchema()
is called with no argument (which unfortunately is the most common usage with Starlight).Considering this, this PR introduces a new explicit type on the
docsSchema()
removing a bit of inference and adding a new short-circuit when it is called with no argument to default toDefaultSchema
.This PR is a draft as I still need to play with it a bit more and make sure it does not break anything but I want to do that with a clear mind after not spending too much time in front of it.How to test?
Simply adding
"composite": true
to thecompilerOptions
of thedocs/tsconfig.json
file and openingdocs/src/content/config.ts
should trigger the error without the changes from this PR.