Skip to content
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

Merged
merged 3 commits into from Apr 30, 2024

Conversation

HiDeoo
Copy link
Member

@HiDeoo HiDeoo commented Apr 22, 2024

What kind of changes does this PR include?

  • Changes to Starlight code

Description

The issue in #1612 is related to having composite set to true in a user tsconfig.json. This option, common for projects using project references, also enables the declaration option, which would generate a TypeScript error in the src/content/config.ts file:

The inferred type of this node exceeds the maximum length the compiler will serialize.
An explicit type annotation is needed.(7056)

Even tho a workaround could be to just disable composite specifically in the Starlight project, this could prevent even type validation using tsc.

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 to DefaultSchema.

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 the compilerOptions of the docs/tsconfig.json file and opening docs/src/content/config.ts should trigger the error without the changes from this PR.

Copy link

changeset-bot bot commented Apr 22, 2024

🦋 Changeset detected

Latest commit: 702b83a

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

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Patch

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

Copy link

vercel bot commented Apr 22, 2024

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

Name Status Preview Updated (UTC)
starlight ✅ Ready (Inspect) Visit Preview Apr 30, 2024 7:38am

@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label Apr 22, 2024
@@ -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]
Copy link
Member Author

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.

@HiDeoo
Copy link
Member Author

HiDeoo commented Apr 26, 2024

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.

Copy link
Member

@delucis delucis left a 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 😁

@delucis delucis added the 🌟 patch Change that triggers a patch release label Apr 29, 2024
* 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)
@HiDeoo
Copy link
Member Author

HiDeoo commented Apr 30, 2024

Needs a changeset though — hopefully if we can describe what this fixes simply, it won’t be as complex as the concept of distributivity 😁

Took a stab at this, hope this makes any sense 😆

Copy link
Member

@delucis delucis left a 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 🎉

@delucis delucis merged commit 6949404 into withastro:main Apr 30, 2024
9 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Apr 30, 2024
HiDeoo added a commit to HiDeoo/starlight that referenced this pull request Apr 30, 2024
* 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)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌟 core Changes to Starlight’s main package 🌟 patch Change that triggers a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Starlight not usable with typescript declarations: true
2 participants