Following TSLint's recommendation to avoid using the var
keyword in TypeScript, I have opted to use import
for importing modules. However, this approach has led to errors when trying to utilize local modules.
In the file routes/main.ts
, my code looks like this:
import express = require("express");
let router = express.Router();
router.get("/", (req, res, next) => { ... });
module.exports = router;
Meanwhile, in app.ts
, my code reads as follows:
/// <reference path="../../typings/main.d.ts" />
import express = require("express");
...
import routes = require("./routes/main");
let app = express();
app.use(routes); // An error occurs at this point.
...
When utilizing import routes
with app.use(routes);
, a type error is generated:
app.ts(36,13): error TS2345: Argument of type 'typeof ".../source/server...'
is not assignable to parameter of type 'RegExp'.
To resolve this issue, I can cast it to a specific type:
app.use(<express.Router>routes);
While this solution works, there are instances where type casting may not be ideal. Alternatively, reverting back to using the var keyword during module importation proves effective:
var routes = require("./routes/main");
let app = express();
app.use(routes); // No errors.
Is there a way to reconcile both the compiler and tslint's guidelines? Should the "no-var-requires": true
setting in tslint be avoided when employing the CommonJS module style?