简体   繁体   中英

TypeScript: How to get inheritance type of class

So I'm writing a compiler, and different "Statement" types have different classes. A Block Statement has a BlockStatement class, an If Statement has an IfStatement class, etc.

I need to be able to tell what type of object I'm working with at runtime, eg

class BlockStatement extends Statement {
    constructor(...children: Statement[]) {
        super()

        this.rep.assemble(
            new BlockLabel(),
            children.map(child => Statement.extractRep(child))
        )
    }

    private addToBlock(pos: number, s: Statement): void {
        this.rep.addChild(pos, Statement.extractRep(s))
    }
}

function prettyPrint(s: Statement) {
    switch () {
        case 'BlockStatement': {
        }

    }
}

How can I tell what type of Statement I am working with? And even if I can tell, will it not matter since it may slice any inherited functionality since the parameter is a statement?

###object###.constructor.name does the trick, however, ive heard minifying may cause issues with this. Then cast itself as whatever it is so intellisense works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM