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

Can't use it with typescript because module not found #105

Closed
stevemarksd opened this issue Mar 23, 2020 · 4 comments
Closed

Can't use it with typescript because module not found #105

stevemarksd opened this issue Mar 23, 2020 · 4 comments

Comments

@stevemarksd
Copy link

I'm getting this error when I check types

image

To reproduce:

import afrom '../a.png?sizes[]=111'

I wonder how all other people are using this library without running into this issue?

@mormahr
Copy link

mormahr commented Apr 22, 2020

You can always ignore warnings with a //@ts-ignore comment before the import.

Otherwise you can create a type definition. It would indeed be nice if there was a definition available, but so far i haven't found one and i'm too lazy to create one myself, so i use it as any.

@Porges
Copy link

Porges commented Aug 6, 2020

I use a definition like this (in react-app-env.d.ts as I am using create-react-app):

type ResponsiveImageOutput = 
    { src: string
    , srcSet: string
    , placeholder: string
    , images: {path: string, width: number, height: number}[]
    , width: number
    , height: number
    }

declare module '*.jpg' {
    const content: ResponsiveImageOutput
    export default content;
}

declare module '*.jpeg' {
    const content: ResponsiveImageOutput
    export default content;
}

declare module '*.png' {
    const content: ResponsiveImageOutput
    export default content;
}

@ajantsch
Copy link

@Porges I tried adding these definitions to my custom.d.ts but the import statement still gives me the Module cannot be found error. Is there anything else necessary to make this work? FYI I'm using webpack, not CRA.

@evoactivity
Copy link

For each set of query params I use I add another module definition to my global.d.ts eg

declare module '*.png' {
  const content: any;
  export default content;
}

declare module '*.png?placeholder=true&sizes[]=300,sizes[]=600,sizes[]=1024,sizes[]=2048' {
  const content: any;
  export default content;
}

Ideally we could use multiple wildcards and just use

declare module '*.png' {
  const content: any;
  export default content;
}

but that would require this issue to be addressed by typescript microsoft/TypeScript#38638

@dazuaz dazuaz closed this as completed Sep 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants