How to check if arbitrary object type is an Image

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

6 Likes

i think you want instanceof?

2 Likes

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

2 Likes

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?

3 Likes

Try this as a workaround:

2 Likes

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

Once I do that your issue should be fixed @UnsignedArduino

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!

3 Likes
4 Likes

what is sarge working on… :face_with_raised_eyebrow: