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

Empty Content Collections with defined scema don't generate typescript types #11075

Open
1 task done
murl-digital opened this issue May 16, 2024 · 8 comments
Open
1 task done
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) feat: content collections Related to the Content Collections feature (scope)

Comments

@murl-digital
Copy link

Astro Info

Astro                    v4.8.4
Node                     v22.0.0
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/tailwind
                         @astrojs/svelte

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I want to publish a blank vesion of my website with no content. I already have a schema set up on config.ts, and doing a dev build complains but works no problem. However, running a full production builds throws many typescript errors (specifically that something's of a type unknown) because the type wasn't properly generated. The example I provided should be enough to get the bug to trigger, but I wasn't able to.

What's the expected result?

In this scenario, I would expect a build with no pages, similar to how a dev build works

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-2n2i41

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label May 16, 2024
@matthewp
Copy link
Contributor

Are you saying that the reproduction doesn't show the issue you found?

@murl-digital
Copy link
Author

Massive egg on my face, but yes. I tried to get it to break yesterday but just couldn't. I am however able to do it with the project I'm working on, I can make the repository public so you can poke around if you like.

@matthewp
Copy link
Contributor

Unless it's relatively small would prefer that you reproduce in the example you provided. It's possible that through reducing you'll find out that it's not a bug at all.

@murl-digital
Copy link
Author

I had to poke it a bit, but I reproduced the issue. Turns out, it's not a problem with the build like I thought, it's a problem with astro check

Reproduction steps are load up the codespace, run astro check, and observe it freak out when the collection's empty

@Princesseuh
Copy link
Member

astro check's behaviour is correct here - when the collection is empty the types are unknown, never etc so trying to do operations on them results in type errors.

@Princesseuh Princesseuh added - P3: minor bug An edge case that only affects very specific usage (priority) feat: content collections Related to the Content Collections feature (scope) and removed needs triage Issue needs to be triaged labels May 21, 2024
@murl-digital
Copy link
Author

astro check's behaviour is correct here - when the collection is empty the types are unknown, never etc so trying to do operations on them results in type errors.

That would make sense if a collection didn't have a schema defined, but since it does the type would be known in theory, I would think

@bholmesdev
Copy link
Contributor

@murl-digital Ah interesting, I am able to replicate with your Stackblitz example by running npm run astro check. Confirmed the issue goes away if at least one entry is present. I agree we should defer to your config types when a directory is empty

@bholmesdev bholmesdev self-assigned this May 22, 2024
@Fryuni
Copy link
Member

Fryuni commented May 29, 2024

astro check's behaviour is correct here - when the collection is empty the types are unknown, never etc so trying to do operations on them results in type errors.

That would make sense if a collection didn't have a schema defined, but since it does the type would be known in theory, I would think

The schema defines the type for the data field in each collection entry.

The type where that field is is inferred from your entries, so that type is unknown. The schema type doesn't even get used.

I think changing it to a generalization of the inferred types would be great, in this case the data field could have the proper types always.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) feat: content collections Related to the Content Collections feature (scope)
Projects
None yet
Development

No branches or pull requests

5 participants