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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that makes sense. I could use duck typing to achieve the same result, though that's messy... is there an alternative?
@Ionix As the type is defined you can only check if specific properties exist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.