Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type '[Images]' is not assignable to type 'string'.ts(2322)

I’m trying to pass array of objects through props but I got that error:

(property) images: [Images]

Type ‘[Images]’ is not assignable to type’string’.ts(2322)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ProductBlock.tsx(4, 5): The expected type comes from this index signature.

Here is my block:

interface Images {
    [key: string]: string;
}

interface Colors {
    [key: string]: string;
}

interface Product {
    _id: string;
    name: string;
    description: string;
    price: number;
    colors: [string];
    images: [Images];
    type: string;
    likes: number;
}

const ProductBlock = (product: Product) => {
    console.log(product.images); // [{...}]

    const RenderColors = () => {

    }
    
    const RenderImages: React.FC<Images> = (images: Images) => {
        console.log(images);
        return(
            <>
                
            </>
        )
    }

    return (
        <div className="product product-container" key={product._id}>
            <RenderImages images={product.images}/> //<--- Error here at "images="
            <h3 className="product-title">{product.name}</h3>
        </div>
    )
}

export default ProductBlock;

>Solution :

product.images is an array of Images objects, but you are passing it to RengerImages which is taking a single Images object as the parameter.

Try this:

const ProductBlock = (product: Product) => {
    console.log(product.images); // [{...}]

    const RenderColors = () => {

    }
    
  const RenderImages: React.FC<{ images: [Images] }> = ({ images }) => {
        console.log(images);
        return(
            <>
                
            </>
        )
    }

    return (
        <div className="product product-container" key={product._id}>
            <RenderImages images={product.images}/> 
            <h3 className="product-title">{product.name}</h3>
        </div>
    )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading