next-optimized-images icon indicating copy to clipboard operation
next-optimized-images copied to clipboard

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

Open JannikZed opened this issue 3 years ago • 12 comments

I was trying to use the canary version with an SVG image and typescript. Getting the following error:

Type 'ImgSrc' is not assignable to type 'string'.ts(2322)
Svg.d.ts(3, 5): The expected type comes from property 'src' which is declared here on type 'IntrinsicAttributes & SvgProps'

The code looks like this:

import { Svg } from 'react-optimized-image';
import Arrow from './arrow-forward-outline.svg';

<Svg src={Arrow} className="arrowforward" />

Seems like the typing is wrong somehow?

JannikZed avatar Aug 04 '20 14:08 JannikZed

I was going to open an issue about it too. I had the exact same problem here, but instead with the Img component.

LucasMallmann avatar Aug 04 '20 18:08 LucasMallmann

@JannikZed there was indeed a wrong typing in the Svg component, thank you very much for reporting it! The version 3.0.0-canary.8 should fix this problem.

@LucasMallmann The typing of the Img component should be correct. Did you add /// <reference types="optimized-images-loader" /> to your next-env.d.ts file? Or do you have any custom typings for image files (maybe from version 2)? If so, you can remove them to use the ones shipped by next-optimized-images.

cyrilwanner avatar Aug 08 '20 12:08 cyrilwanner

Screen Shot 2020-08-20 at 10 43 07 PM

There's an issue with typescript and the Img src prop.

arnonate avatar Aug 21 '20 03:08 arnonate

I figured this out. I forgot to require the image src. Using require(ImgSrc) fixes the types issue.

arnonate avatar Aug 21 '20 15:08 arnonate

I figured this out. I forgot to require the image src. Using require(ImgSrc) fixes the types issue.

Explain how you did it?

ilyariver avatar Jul 06 '22 15:07 ilyariver

I did with

<img src={URL.createObjectURL(file?.[0])} alt="" className={styles.img} />

mjahmed-wd avatar Aug 11 '22 13:08 mjahmed-wd

I figured this out. I forgot to require the image src. Using require(ImgSrc) fixes the types issue.

Hi! How did you do that? Showing your code will be helpful

RuwaidMuntajim avatar Aug 16 '22 18:08 RuwaidMuntajim

{URL.createObjectURL(file?.[0])}

Isn't it a bad way to do this. I seem to have heard that we should revokeObjectURL after createObjectURL otherwise there'll be memory leak. Am i right?

AxelReid avatar Aug 25 '22 06:08 AxelReid

require(ImgSrc)

How did you do this? Because I have the same problem

mrparkersson avatar Aug 28 '22 10:08 mrparkersson

I Just update my code from

import imageX from './assets/image.jpg'
<img src={imageX />

to

import imageX from './assets/image.jpg';
<img src={imageX.src} />

this working for me

pedrojobs13 avatar Feb 23 '23 05:02 pedrojobs13

Did you figure why it works?

netsamma avatar Jul 08 '23 20:07 netsamma

I Just update my code from

import imageX from './assets/image.jpg'
<img src={imageX />

to

import imageX from './assets/image.jpg';
<img src={imageX.src} />

this working for me

Yo typescript is weird af 💀

IntiSilva avatar Oct 02 '23 16:10 IntiSilva