Why doesnt this work :|

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) {
        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!"
}

game.splash(msg)
3 Likes