prismicio / prismic-types

Type definitions for Prismic content, models, and APIs
https://prismic.io/docs/technologies/javascript
Apache License 2.0
11 stars 4 forks source link

Array types are incompatible #54

Closed Laruxo closed 1 year ago

Laruxo commented 1 year ago

Is your feature request related to a problem? Please describe.

I am a bit frustrated with the use of this pattern:

State extends "empty" ? [] : [RTNode, ...RTNode[]]

In my code, I have custom types holding data like nodes: RTNode[] or slices: Slice[]. However, these types are not compatible with [] | [RTNode, ...RTNode[]].

I would love to hear more context about this pattern and why did you decide to use it instead of never[] | RTNode[].

Describe the solution you'd like

Ideally, Typescript would understand that [] | [RTNode, ...RTNode[]] is equivalent to RTNode[] (both ways). Until then I would prefer simpler types without this State (e.g. never[] | RTNode[])

github-actions[bot] commented 1 year ago

This issue has been labeled as a feature request since it was created using the 🙋‍♀️ Feature Request Template.

Hi there, thank you so much for your request!

Following our Maintenance Process, we will review your request and get back to you soon. If we decide to implement it, will proceed to implement the feature during the last week of the month. In the meantime, feel free to provide any details to help us better understand your request, such as:

If you think you can implement the proposed change yourself, you're more than welcome to open a pull request implementing the new feature. Check out our quick start guide for a simple contribution process. Please note that submitting a pull request does not guarantee the feature will be merged.

- The Prismic Open-Source Team

angeloashmore commented 1 year ago

Hi @Laruxo, I'm sorry for the late reply.

State extends "empty" ? [] : [RTNode, ...RTNode[]] was preferred over RTNode[] because it more explicitly defines what happens when a field is empty or filled.

The State type parameter allows the types to be smart enough to tell you when you are passing a field value into something that expects a filled value. @prismicio/client's isFilled helpers (e.g. isFilled.image(imageField)), lets you narrow the type to a filled or empty value. That narrowing could help write stronger code that is type checked. If you don't use the isFilled helpers, should still be able to conditionally access properties by performing other runtime checks.

We recommend that you use the field-level types from @prismicio/client rather than the lower-level types like RTNode and Slice[]. For example, you can use RichTextField and SliceZone instead.

I'll leave this topic open for now in case you or anyone else would like to share other viewpoints. What I explained above details our original design, but that doesn't mean we can't change it. 🙂

lihbr commented 1 year ago

Closing as stale, happy to reopen if anyone wants to continue the discussion~

Thanks for contributing!