How to create an instanceof AST node using typescript compiler API

162 views Asked by At

I would like to represent the typescript code "MyObj instanceOf MyClass" with the typescript compiler API.

I try to use the createBinary API as follow:

ts.createBinary(leftExpression,ts.SyntaxKind.InstanceOfKeyword, rightExpression);

How do I create the rightExpression?

1

There are 1 answers

0
David Sherret On

The left and right expressions are identifiers, so the createIdentifier method could be used:

const binaryExpression = ts.createBinary(ts.createIdentifier("MyObj"),
    ts.SyntaxKind.InstanceOfKeyword,
    ts.createIdentifier("MyClass"));