Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript Issue using example from Docs #1214

Closed
ErikAGriffin opened this issue Apr 22, 2023 · 2 comments · Fixed by #1241
Closed

Typescript Issue using example from Docs #1214

ErikAGriffin opened this issue Apr 22, 2023 · 2 comments · Fixed by #1241
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@ErikAGriffin
Copy link

Hi, I set up my repository to try the example from the docs:

import { graphql } from 'gatsby'
import React from 'react'

import Gallery from '@browniebroke/gatsby-image-gallery'

const MyPage = ({ data }) => {
  const images = data.allFile.edges.map(({ node }) => node.childImageSharp)
  // `images` is an array of objects with `thumb` and `full`
  return <Gallery images={images} />
}

export const pageQuery = graphql`
  query ImagesForGallery {
    allFile {
      edges {
        node {
          childImageSharp {
            thumb: gatsbyImageData(
              width: 270
              height: 270
              placeholder: BLURRED
            )
            full: gatsbyImageData(layout: FULL_WIDTH)
          }
        }
      }
    }
  }
`

export default MyPage

And I'm getting the following Typescript error on the line where images is passed into Gallery. It doesn't like that the images array may contain null values, because the images prop is expecting an array of only ImageProp, and the GraphQL query thinks that the nodes may be null. Any suggestions on how to fix this?

Type '({ readonly thumb: IGatsbyImageData; readonly full: IGatsbyImageData; } | null)[]' is not assignable to type 'ImageProp[]'.
  Type '{ readonly thumb: IGatsbyImageData; readonly full: IGatsbyImageData; } | null' is not assignable to type 'ImageProp'.
    Type 'null' is not assignable to type 'ImageProp'.
@ErikAGriffin
Copy link
Author

Hmm I ended up having to create my own types file to export the ImageProp interface, and doing the following:

  const images: ImageProp[] = [];
  data.allFile.nodes.forEach(node => {
    if (node.childImageSharp) {
      images.push(node.childImageSharp);
    }
  });

It would be nice if Gallery exported its types, or if the images prop handled receiving potentially null values for childImageSharp data from GraphQL

@browniebroke
Copy link
Owner

browniebroke commented Apr 23, 2023

Your suggestion makes sense, we should indeed export our types. Not sure if I'll have time to do it myself, but if you submit a pull request, I'll be more than happy to review it.

@browniebroke browniebroke added enhancement New feature or request help wanted Extra attention is needed labels Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants