1

Consider this basic node+ts code:

import * as express from "express";

function doStuff(app: express.Application) {
  if (!(app instanceof express.Application)) throw new TypeError();
  // etc...
}

VSCode shows me this error for ...instanceof express.Application...:

[ts] Property 'Application' does not exist on type 'typeof e'. [2339]

What am I doing wrong?

1 Answer 1

4

instanceof is a Javascript operator. The right hand side operand must be a runtime value (a function or a constructor) for it to work.

express.Application is defined as an interface. Interfaces only exist at compile time to aid in type checking. This means at runtime, there is no express.Application value to be the operand in the instanceof operation, and so typescript issues an error.

2
  • Thanks that makes sense. I could use duck typing to achieve the same result, though that's messy... is there an alternative?
    – lonix
    Commented Jan 20, 2019 at 8:10
  • 1
    @Ionix As the type is defined you can only check if specific properties exist Commented Jan 20, 2019 at 8:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.