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

`SharedSliceVariation` doesn't have `slice_type` #44

Closed Klynger closed 2 years ago

Klynger commented 2 years ago

Versions

Reproduction

image

Steps to reproduce

Just use the type

What is expected?

The type to have a definition for slice_type.

What is actually happening?

The type doesn't have a definition for slice_type

github-actions[bot] commented 2 years ago

This issue has been labeled as a bug since it was created using the 🚨 Bug Report Template.

Hi there, thank you so much for the report!

Following our Maintenance Process, we will review your bug report and get back to you next Wednesday. To ensure a smooth review of your issue and avoid unnecessary delays, please make sure your issue includes the following:

If you have identified the cause of the bug described in your report and know how to fix it, you're more than welcome to open a pull request addressing it. Check out our quick start guide for a simple contribution process.

If you think your issue is a question (not a bug) and would like quicker support, please close this issue and forward it to an appropriate section on our community forum: https://community.prismic.io

- The Prismic Open-Source Team

angeloashmore commented 2 years ago

Hi @Klynger,

Thanks for reporting this issue. This is actually intentional; SharedSliceVariation is intended to be used along with SharedSlice. SharedSlice contains slice_type and accepts a union of SharedSliceVariation types.

You can see the SharedSlice type definition here: https://github.com/prismicio/prismic-types/blob/f7c39e38ead6aae8527dd07259359f6fab610c9f/src/fields.ts#L820-L826

You would use it like this:

import * as prismicT from '@prismicio/types'

type ImageSlice = prismicT.SharedSlice<
  "image",
  ImageSliceDefault | ImageSliceWithCaption
>

type ImageSliceDefault = prismicT.SharedSliceVariation<
  "default",
  {
    image: prismicT.ImageField;
  },
  never
>

type ImageSliceWithCaption = prismicT.SharedSliceVariation<
  "withCaption",
  {
    image: prismicT.ImageField;
    caption: prismicT.RichTextField;
  },
  never
>

We just released a new tool call prismic-ts-codegen that can generate all of these types for you automatically. I recommend using that tool over writing the types by hand.

# Install the codegen tool
npm install --save-dev prismic-ts-codegen @prismicio/types

# Create a prismicCodegen.config.ts configuration file
npx prismic-ts-codegen init

# Generate types
npx prismic-ts-codegen

You can learn more about the tool here: https://prismic.io/docs/technical-reference/prismic-ts-codegen

Klynger commented 2 years ago

That's amazing! Thank you, @angeloashmore