Reputation: 319
I am running Node v12 and Express v4.16.4 and Typescript version 3.8.3 and using VSCode.
This piece of code has been unchanged for almost 8 months and we use it in every single router.
export interface ICustomRequest extends Request {
district: string;
}
And it has worked fine all of these months. Every other person on my team is using the same code as me and is still having no issues with it. All of a sudden this morning--none of my code will compile due to an error involving req.query in all of the routers.
Things such as this:
public async get(req: ICustomRequest, res: Response, next: NextFunction) {
try {
const property = req.query.property;
let results: await doTheThing(property);
res.json(results);
} catch (error) {
next(error);
}
}
Where req.query.property throws an error such as: "error TS2345: Argument of type 'string | Query | (string | Query)[]' is not assignable to parameter of type 'string'. Type 'Query' is not assignable to type 'string'."
I don't understand how all of a sudden this is an issue across the board in every file in my project while the other three developers do not have this issue and we are all on the same versions of everything. If I add .toString() it works, but I can't justify modifying 202 instances for just myself when the others aren't having issues. Any ideas about my environment that would cause these typing issues?
Things I have tried:
Completely re-cloning my projects from upstream. Reinstalling node. Reinstalling typescript. npm cache clean. removing node modules and reinstalling. removing package-lock.json.
Upvotes: 2
Views: 448
Reputation: 21
Getting it work, setting fix versions for express, @type/express and @type/express-serve-static-core:
"dependencies": {
"express": "4.16.4",
},
"devDependencies": {
"@types/express": "4.16.1",
"@types/express-serve-static-core": "4.16.4",
}
Commit shrinkwrap.json or package-lock.json to your repository to aviod such problems in the future.
Upvotes: 2
Reputation: 4499
That's typical problem when installing "@types" packages that has no strictly defined dependencies. NPM has installed newest version of @types/express
I guess.
Try to downgrade to 4.16.11 or higher (you can try them one by one).
"devDependencies": {"@types/express": "4.16.11"}
Lesson to the future. Always have proper package-lock.json committed to repository. This should prevent such kind of issue.
Upvotes: 2