Skip to content

sys.contentType.sys.id string is not assignable to string literal #56

Open
@builtbyjay

Description

@builtbyjay

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[]
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions