81

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?:   ??
}
4
  • How do you represent the image? Is it an URL to an image? Is it blob data? Is it a HTML <img> element? Commented Jan 22, 2017 at 14:18
  • It's a some kind of file upload image. Commented Jan 22, 2017 at 14:21
  • So you grab an image from a file input? Commented Jan 22, 2017 at 14:25
  • yes. I will grab from file input Commented Jan 22, 2017 at 14:29

2 Answers 2

148

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

Sign up to request clarification or add additional context in comments.

3 Comments

It's recommended to use "string" as type and not "String" for this usage.
If the image is a byte array, would you recommend using HTMLImageElement or File?
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 stackoverflow.com/questions/20756042/… and stackoverflow.com/questions/35038884/…
4

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.

1 Comment

Just tested this in my code and your comment is correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.