Categories
TypeScript Answers

How to import image with TypeScript?

Spread the love

To import image with TypeScript, we can use declare to declare the type for image files.

For instance, we write

declare module "*.png" {
  const value: any;
  export = value;
}

to declare the type for modules with the .png extension.

We just allow any property to be exported with

const value: any;
export = value;

And then we can import .png files without errors since png files are recognized by the TypeScript compiler after we added the module declaration.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *