`getImage` types requires `Node` properties in it
See original GitHub issuePreliminary Checks
- This issue is not a duplicate. Before opening a new issue, please search existing issues: https://github.com/gatsbyjs/gatsby/issues
- This issue is not a question, feature request, RFC, or anything other than a bug report directly related to Gatsby. Please post those things in GitHub Discussions: https://github.com/gatsbyjs/gatsby/discussions
Description
Hi, I’ve the following code where getImage
is throwing this error:
Argument of type '{ childImageSharp: { gatsbyImageData: any; }; }' is not assignable to parameter of type 'ImageDataLike'.
Type '{ childImageSharp: { gatsbyImageData: any; }; }' is not assignable to type 'FileNode'.
Type '{ childImageSharp: { gatsbyImageData: any; }; }' is missing the following properties from type 'Node': parent, children, internal, idts(23
import React from "react";
import { graphql, PageProps } from "gatsby";
import { GatsbyImage, getImage } from "gatsby-plugin-image";
interface IProps {
hero: {
childImageSharp: {
gatsbyImageData: any;
};
};
}
const HomePage = ({ data }: PageProps<IProps>) => {
const image = getImage(data.hero!); // error here
if (!image) {
return null;
}
return <GatsbyImage image={image} alt="hero" />;
};
export const query = graphql`
query IndexPage {
hero: file(relativePath: { eq: "home/hero.png" }) {
childImageSharp {
gatsbyImageData(layout: FULL_WIDTH, placeholder: BLURRED, quality: 100)
}
}
}
`;
export default HomePage;
My understanding from this error is that I need to include in the query and its interface, the Node properties 'Node': parent, children, internal, id
.
Is this error expected? what is the recommended way of fixing this ts error?
Thanks in advance!
Reproduction Link
https://codesandbox.io/s/get-image-typing-membno?file=/src/pages/index.tsx
Steps to Reproduce
- From a TS project (or https://github.com/gatsbyjs/gatsby-starter-minimal-ts) +
gatsby-plugin-image
- Copy the code in the description
- Check the type error in the
getImage
function
Expected Result
getImage
type should not require Node properties in it.
Actual Result
getImage
type requires Node properties in it.
Environment
System:
OS: Linux 5.4 Debian GNU/Linux 10 (buster) 10 (buster)
CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
Shell: 5.0.3 - /bin/bash
Binaries:
Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node
Yarn: 1.22.17 - ~/.nvm/versions/node/v14.18.1/bin/yarn
npm: 6.14.15 - ~/.nvm/versions/node/v14.18.1/bin/npm
Languages:
Python: 2.7.16 - /usr/bin/python
npmPackages:
gatsby: ^4.14.1 => 4.14.1
gatsby-plugin-image: 2.14.1 => 2.14.1
Config Flags
None
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Node get image properties (height, width) - Stack Overflow
I'm looking for a means to get the height and width of images from a given path locally. I know about imagemagick and...
Read more >Get Image Method - NI - National Instruments
Short Name: Get Image. ... create a Property Node from any terminal on the block diagram of the ... Data type, Name, Required,...
Read more >How to fetch images from Node.js server ? - GeeksforGeeks
Example: The following code is an example of how to get an image or other static files from the node server. Filename: server.js....
Read more >ImageView (JavaFX 8) - Oracle Help Center
The ImageView is a Node used for painting images loaded with Image class. This class allows resizing the displayed image (with or without...
Read more >Processing images with sharp in Node.js - LogRocket Blog
This module can produce images in JPEG, PNG, WebP, AVIF, and TIFF formats as well as uncompressed raw pixel data. In this tutorial,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@LekoArts I noticed that there is still an issue with this when using Gatsby’s GraphQL Typegen feature. All the image values are nullish by default and
getImage
is expectingundefined
instead.Update: just read your prev comment about this problem. Would be a good addition, yes.
This means that if you pass a
node
ornode.childImageSharp
etc. and it’snull
it doesn’t crash but returnsundefined
from thegetImage
helper. But our typegen feature types it asIGatsbyImageData | null
so we could add it.Ok, I think I fixed it. This here doesn’t have any type errors: