How do I use express.Router in ts and eslint without error

Viewed 530

I have tried to export a controller like below

export const IndexController: Router = Router();

I get the following error:

A function with a name starting with an uppercase letter should only be used as a constructor.eslintnew-cap

I have tried to preface this with the 'new' operator and get the following error.

'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.

The only solution I know of is to add the comment eslint-disable-next-line new-cap but this isn't viable and I don't want to paste this every time I want to export a controller.

2 Answers

You can use the capIsNewExceptions option which:

allows specified uppercase-started function names to be called without the new operator.

Put it in your ESLint config with:

"eslint new-cap": ["error", { "capIsNewExceptions": ["IndexController"] }]

Disable by adding this comment:

/* eslint new-cap: ["error", { "capIsNewExceptions": ["Router"] }] */
const routes = express.Router()
Related