Replies: 3 comments 4 replies
-
Hi. You're importing the default from "gatsby-image", which is the component itself, not the type for the props. You want the named export import Img, { GatsbyImageProps } from "gatsby-image"
import React from "react";
import Img from "gatsby-image";
import styled from "styled-components";
import GatsbyImage from "gatsby-image";
interface HeroImageProps {
className?: string;
image: GatsbyImageProps;
heroposition: "left" | "right";
}
//etc |
Beta Was this translation helpful? Give feedback.
-
I have the same issue. Here is my code.
|
Beta Was this translation helpful? Give feedback.
-
I think this is expected behavior for non-discriminated unions:
We probably need to add a discriminator field for proper handling in this case. A pull request is welcome! In the meantime, you can use guards as a hacky workaround: type ObjectWithFluid = { fluid: FluidObject | FluidObject[] }
function isFluid(x: GatsbyImageProps): x is ObjectWithFluid {
return x.hasOwnProperty(`fluid`)
}
const HeroImage: React.FC<HeroImageProps> = ({
className,
heroposition,
image,
}) => {
if (!isFluid(image)) {
throw new Error(`Not fluid`)
}
return (
<FullWidthImage
className={className}
fluid={image.fluid}
heroposition={heroposition}
/>
);
}; |
Beta Was this translation helpful? Give feedback.
-
#24336
I have searched existing issues and posted on StackOverflow with the
gatsby
tag, to no avail.However, I am well aware this could be just me being a total beginner - whilst I have a couple of years experience with Gatsby, I am brand new to TypeScript.
Essentially, the issue is that I am typing props on a component. One of these properties:
image: GatsbyImage
accepts fluid image data, pulled in via GraphQL from my CMS (Contentful). The data itself is coming in fine; the image is displaying as expected.But instead of accepting a
fluid
prop when I render the component with JSX, it displays the error in the title/described below.I have tried swapping out type
GatsbyImage
for all the other types in the declaration file, but none of them seem to prevent this error.Since I don't have the knowledge to conclusively state that the typings for
gatsby-image
are correct, I have opened this as a potential bug report, but please do correct me.Steps to reproduce
I can create a reproduction repo if this is really required but I will hold off on investing that effort until I am sure I am not just making a phenomenally stupid mistake. Here is the relevant code:
The Gatsby Page I am querying for data on, and including the component on:
The component itself:
It is right at the end, inside the
FullWidthImage
fluid
prop where the error appears.Expected result
When declaring the
GatsbyImage
type on an interface prop that expects afluid
object input, TypeScript verifies that propertyfluid
exists on typeGatsbyImage
.Actual result
Error:
TypeScript error tells me “Property 'fluid' does not exist on type 'GatsbyImage'”
Environment
Beta Was this translation helpful? Give feedback.
All reactions