1

I have a TypeScript 2 file in which I'm attempting to make use of an image like this:

import image = require("../assets/images/image.jpg");

However, this throws

[at-loader] ./src/components/app.tsx:3:30 
    TS2532: Object is possibly 'undefined'.

What is the recommended way to make use of an asset without the risk of it being undefined? Note that I don't want to suppress the warning.

langkilde
  • 1,473
  • 1
  • 20
  • 37
  • Possible duplicate of [How to suppress TypeScript "error TS2533: Object is possibly 'null' or 'undefined'"?](https://stackoverflow.com/questions/40349987/how-to-suppress-typescript-error-ts2533-object-is-possibly-null-or-undefine) – Kukic Vladimir Jul 09 '17 at 21:01
  • @KukicVladimir Thanks! But I don't want to suppress it. Checking for possibly undefined variables is useful. It feels like there should be a way to import an asset without throwing this error. – langkilde Jul 09 '17 at 21:04

1 Answers1

-1

import image = require("../assets/images/image.jpg");

You cannot import an image file like this.

You probably wanted to do img with a src set to the image tag.

More

There are indeed ways to import an image file with webpack bundling e.g. using raw file loader, but I doubt that is what you are doing here.

basarat
  • 261,912
  • 58
  • 460
  • 511