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

Typegen: coalesce with a string fallback still includes null as part of generated type #7888

Open
LorinRenodeyn2PO opened this issue Nov 27, 2024 · 0 comments

Comments

@LorinRenodeyn2PO
Copy link

If you find a security vulnerability, do NOT open an issue. Email [email protected] instead.

Describe the bug

Based on the Groq cheat sheet and the reference, I understand the coalesce() function to go over all its arguments and return the first one that isn't null. Passing in a string as final argument should therefore result in a value always being available.

When executing queries and also in the vision area of the studio, this works as expected. However, the typgen will still generate null as part of the type for such fields. Possibly in cases where the field one of the params references does not exist?

For ease of looping I've added this field to all of the models (blocks, images, documents, ...) in our queries: "id": coalesce(_id, _key, "id_unknown").

Depending on the model & circumstances (e.g. included in rich text or not) the _id and/or _key fields might not be present.
When executing queries both in our FE and in the vision, this neatly falls back to "id_unknown".
But in typegen this results in cases like this:

// query
export const siteWideQuery = groq`{
	"header": *[_type == "navigation.header" && locale == $locale && $channel in channels][0]{
		"id": coalesce(_id, _key, "id_unknown"),
		_type,
		...
	}
}

// generated type
export type SiteWideQueryResult = {
  header: {
    id: string | null | 'id_unknown';
   /* more fields */
  }
}

To Reproduce

Steps to reproduce the behavior:
Already mentione in description

Expected behavior

If the coalesce function is provided a non-dynamic fallback value, typegen should not include null in the possible types of the field to which coalesce is applied.

Screenshots
If applicable, add screenshots to help explain your problem.

Which versions of Sanity are you using?

Run sanity versions in the terminal and copy-paste the result here.

What operating system are you using?

Which versions of Node.js / npm are you running?

Run npm -v && node -v in the terminal and copy-paste the result here.

Additional context

Add any other context about the problem here.

Security issue?

no

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

No branches or pull requests

1 participant