# How to check if arbitrary object type is an Image

**URL:** https://forum.makecode.com/t/how-to-check-if-arbitrary-object-type-is-an-image/37926
**Category:** Help
**Created:** [July 7, 2025, 11:25pm UTC](https://forum.makecode.com/t/how-to-check-if-arbitrary-object-type-is-an-image/37926 "2025-07-07T23:25:51Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [July 7, 2025, 11:25pm UTC](https://forum.makecode.com/t/how-to-check-if-arbitrary-object-type-is-an-image/37926/1 "2025-07-07T23:25:51Z")

</div>

I need to check if an object passed to a function as an argument of type any is an image. Basically, I need to pass the argument to different function depending on the type. I use the `typeof` keyword for all the basic JS types like number and string etc. and I have a separate check in case this returns ‘object’.

When an object is detected, the Array namespace has an isArray function I can use to recognise the type as array and pass it on, however I have found no way to differentiate any other object subtypes. I’m forced to pass the argument on to a generic functions for objects which does not work for my purpose (in this case checking equality).

Afaik the image namespace does not have such a function for checking types, `isinstance` cannot be used as ‘Image’ is treated as a type, not a class, and I think checking properties or the constructor to deduce type is not possible in MakeCode. Does anyone know some kind of alternate method or workaround for this? @richard? Thanks

---

<div class="post-metadata">

### Author: ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)
#### Post date: [July 14, 2025, 4:39pm UTC](https://forum.makecode.com/t/how-to-check-if-arbitrary-object-type-is-an-image/37926/2 "2025-07-14T16:39:45Z")

</div>

i think you want `instanceof`?

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [July 22, 2025, 4:27pm UTC](https://forum.makecode.com/t/how-to-check-if-arbitrary-object-type-is-an-image/37926/3 "2025-07-22T16:27:05Z")

</div>

My bad that’s what I meant by isinstance (python habits) but yeah MakeCode treats Image as a type so I can’t use it as a value

---

<div class="post-metadata">

### Author: ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)
#### Post date: [July 22, 2025, 6:26pm UTC](https://forum.makecode.com/t/how-to-check-if-arbitrary-object-type-is-an-image/37926/4 "2025-07-22T18:26:01Z")

</div>

i see what you mean. i guess the only way to do this would be for us to implement some sort of `Image.isImage()` function. could you open an issue?

---

<div class="post-metadata">

### Author: ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)
#### Post date: [July 22, 2025, 10:58pm UTC](https://forum.makecode.com/t/how-to-check-if-arbitrary-object-type-is-an-image/37926/5 "2025-07-22T22:58:56Z")

</div>

Try this as a workaround:

> [@Why doesnt this work expressionless](https://forum.makecode.com/t/why-doesnt-this-work/12991/3):
>
> Interesting, @hasanchik . Image is an interface, not a class, and instanceof does not work with interfaces in TypeScript. The traditional way to deal with this is to typecast the variable and check for an interesting method. That, though, does not work in MakeCode. [image] So, the next best thing is to check for an interesting attribute instead. This works: let a: Image = img`.` let b: number = 42 let msg: string = "" function testImage(i: any): boolean { if ((\<Image\>i).width) { …

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [July 23, 2025, 12:20pm UTC](https://forum.makecode.com/t/how-to-check-if-arbitrary-object-type-is-an-image/37926/6 "2025-07-23T12:20:31Z")

</div>

That worked, thanks! I just updated my equality library, now I just have to sync it in every extension that uses it 🫠

Once I do that your issue should be fixed @UnsignedArduino

> [@richard](#):
>
> could you open an issue?

Sure, I’ll get to it. I think it would be a nice feature to have alongside the Array one, although its use case is pretty niche like right now. Thanks everyone for your help!

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [July 28, 2025, 10:08pm UTC](https://forum.makecode.com/t/how-to-check-if-arbitrary-object-type-is-an-image/37926/7 "2025-07-28T22:08:36Z")

</div>

> <https://github.com/microsoft/pxt-arcade/issues/6973>
>
> \*\*Is your feature request related to a problem? Please describe.\*\*
> Checking whet…her an arbitrary object (any) is an Image type is unnecessarily hard as opposed to arrays which have a function conveniently built-in to the Array namespace.
> 
> \*\*Describe the solution you'd like\*\*
> An \`isImage\` method that returns true if an arbitrary object is an Image type.
> 
> \*\*Describe alternatives you've considered\*\*
> It is currently (afaik) only possible to deduce that an object is an image by casting it to an Image type and checking for an attribute present on Image objects (like width) as demonstrated below. Checking for type or instance doesn't work on Image types.
> 
> \`\`\`js
> function isImage(obj: any): boolean {
> if ((obj as Image).width) return true;
> return false;
> }
> \`\`\`
> https://forum.makecode.com/t/why-doesnt-this-work/12991/3?u=sarge

---

<div class="post-metadata">

### Author: ![Josef](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/josef/32/12457_2.png) [@Josef](https://forum.makecode.com/u/Josef)
#### Post date: [July 29, 2025, 4:53pm UTC](https://forum.makecode.com/t/how-to-check-if-arbitrary-object-type-is-an-image/37926/8 "2025-07-29T16:53:23Z")

</div>

what is sarge working on… 🤨
