73

I'm declaring an interface which will contains image also. What type do i need to give to it.

export interface AdInterface {
    email: string;
    mobile: number;
    image?:   ??
}
Vicientes
  • 3
  • 1
  • 6
Seenu S
  • 3,381
  • 6
  • 30
  • 45

2 Answers2

129

If your image property contains ...

Image used as <img> element

image?: HTMLImageElement

URL to image.

image?: String

Image as file from <input> element

image?: File

Misaz
  • 3,694
  • 2
  • 26
  • 42
  • 10
    It's recommended to use "string" as type and not "String" for this usage. – Gabriel Petersson Jul 24 '20 at 22:36
  • If the image is a byte array, would you recommend using HTMLImageElement or File? – abhi Jan 13 '22 at 16:33
  • 1
    Type of byte array is `array`. If you need work with `HTMLImageElement` or `File` instead then you need to convert your byte array by conversion specific procedure depending on your needs. Conversions are usualy done by converting to base64 string. More details and examples are at https://stackoverflow.com/questions/20756042/how-to-display-an-image-stored-as-byte-array-in-html-javascript and https://stackoverflow.com/questions/35038884/download-file-from-bytes-in-javascript – Misaz Jan 14 '22 at 10:25
2

Most image when imported into server-side TypeScript are strings.

For example trying to reference File or HTMLImageElement on an imported image you will get this error:

TS2322: Type 'string' is not assignable to type 'HTMLImageElement'.

I recommend just using string. If you are having an import error for PNG images in TypeScript then take a look here.