Why doesnt this work :|

if (a instanceof Image) {


why doesn’t this work :expressionless:

‘Image’ only refers to a type, but is being used as a value here

Why do you want to know a variable’s type?

Variables are restricted to one* type only.

* Well, unless you need to use Any for some reason.

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.


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) {
        return true
    } else {
        return false

if (testImage(a)) {
    msg += "a is an image!"
} else {
    msg += "a is not an image"

if (testImage(b)) {
    msg += "; b is an image!"
} else {
    msg += "; b is not an image!"

1 Like

Didn’t realize all those drawicon errors from before could teach me anything.

Also didn’t realize a image was a interface.

1 Like

…i have my reasons