contentful-typescript-codegen icon indicating copy to clipboard operation
contentful-typescript-codegen copied to clipboard

`sys.contentType.sys.id` string is not assignable to string literal

Open builtbyjay opened this issue 4 years ago • 0 comments

Hello 😄 first of all, amazing package, thank you for releasing and maintaining it.

I've noticed that when I try and define the return type of the getStaticProps() function (Next.js), if I use an interface that defines the contentType I get a typescript error on the contentType.sys.id property, complaining that type string is incompatible with the string literal (in my example, "city").

Here's the code snippet:

interface Props {
  cities: ICity[];
}

export const getStaticProps: GetStaticProps<Props> = async context => {
  const client = createClient({
    space: process.env.CONTENTFUL_SPACE,
    accessToken: process.env.CONTENTFUL_TOKEN
  });

  const cities = await client.getEntries<ICityFields>({
    content_type: 'city'
  });

  return {
    props: {
      cities: cities.items
    }
  }
}

And a snapshot of the error:

Screen Shot 2021-02-04 at 11 38 01

For now, I'm asserting the type in the return statement to make the error go away - which isn't much of an inconvenience tbh. Is this the correct solution?

  return {
    props: {
      cities: cities.items as ICity[]
    }
  }

builtbyjay avatar Feb 04 '21 11:02 builtbyjay