prismicio / prismic-react

React components and hooks to fetch and present Prismic content
https://prismic.io/docs/technologies/homepage-reactjs
Apache License 2.0
154 stars 40 forks source link

Add minimal Typescript example with repeatable fields #125

Closed beamercola closed 2 years ago

beamercola commented 2 years ago

I'm having a really tough time getting typescript to be ok with my slices. The custom-slicezone-props example was helpful, however looping through nested items I can't seem to figure out.

angeloashmore commented 2 years ago

Hey @beamercola, could you provide a short example of what you're trying to do? I can write up an example if you point out what isn't working.

(By the way, I'm working on a CLI that will generate TypeScript types for you automatically that you might be interested in. We'll share publicly once it's available - hopefully soon.)

angeloashmore commented 2 years ago

If you're looking for an example of how to type the repeatable fields in a Slice, you can provide the fields as the third type parameter to prismicT.Slice:

export type HeroSlice = prismicT.Slice<
    "hero",
    {
        heading: prismicT.KeyTextField;
        text: prismicT.RichText;
    },
    {
        buttonLink: prismicT.LinkField;
        buttonText: prismicT.KeyTextField;
    }
>;

This will type the items field as an array of objects with those properties.

(In addition to the CLI I'm working on, we're also working on writing/generating documentation for the TypeScript Types. They aren't documented today and we recognize that documenting them will be helpful.)

angeloashmore commented 2 years ago

@beamercola I added a TypeScript-specific example here: https://github.com/prismicio/prismic-react/tree/master/examples/with-typescript

I'm going to close this issue, but please let me know if you are looking for something specific in the example. I'm more than happy to update it. :)

beamercola commented 2 years ago

Amazing, thank you!