Is there a "use explicit type refactoring" in VSCode/TypeScript?

Viewed 214
1 Answers

EDIT:

To get the types of a function return you can use the ReturnType type constructor. Combine that with type indexing using bracket notation and you can find the return type of an interface member like so:

type Server = ReturnType<Express['listen']>;

The Node standard module http exposes a type called Server. Since Express uses this type for its server listeners, you can use it to type the listener on your member like so:

import type { Express } from "express";
import type { Server } from "http";

class App {
  constructor(app: Express) {
    this.listener = app.listen();
  }
  private listener: Server;
}

If you want to use a custom listen() function with an uninitialized server variable as you're doing in your above code snippet, you just need to make the property optional:

class App {
  private server?: Server;
  listen(expressApp: Express) {
    this.server = expressApp.listen()
  }
}
Related