-
-
Notifications
You must be signed in to change notification settings - Fork 532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't extend express Request type #745
Comments
What’s your |
I also tried with |
@brunolm That wouldn't work because it'd just discover the first one, not all of them. You can always use the |
Oh, also, you probably should just put your overrides in a different folder and it would be a non-issue. The problem is the conflict between two Express packages being resolved, but you can create a different package named something to do with your app or anything else that doesn’t conflict. |
As this is confusing a lot of people to get right, I'd suggest setting up a working example in the docs |
I don't fully understand why that's required now. I can confirm that this worked before: versions
tsconfig.json {
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": false,
"sourceMap": true
}
} typings/foo.d.ts declare module Express {
export interface Request {
knex: any;
config: any;
client: { id: number; name: string; };
}
} |
Please see the README and CHANGELOG, and possibly search past issues to find why it was changed. |
There is also an example in the README for adding custom types and modules. If you’d like to expand upon it, please feel free to submit a PR! |
Just checked and we should invert the resolution order of types so it works as expected here. Edit: The reason for the change is also in the README. |
With Can you guys @3mard @blakeembrey tell WHY this works? I would like understand more :) Also is this the best solution for this problem or is it only a workaround? |
I gave up and started to do this: req['token'] = 'foo' And then casting to something when needed. |
@henrikra I think this is a design decision that was made to optimize ts-node (why include .d.ts files that your app are not using anyway ?) https://github.com/TypeStrong/ts-node/blob/master/src/index.ts#L481 |
@brunolm @henrikra I created this repo example https://github.com/3mard/ts-node-example for extending express request |
Thanks to this comment by @blakeembrey, I got my declaration merges working! All you need to do is put "typeRoots": ["./typings", "./node_modules/@types"] |
@saoudrizwan Thanks it works :) |
This worked for me:
declare module 'express-serve-static-core' {
interface Request {
foo?: string
}
} |
@marcosfede your code makes everything be an If I delete your file the types work again. |
Same. I can't make it work. Neither solution helps. Yours is an ugly hack but works for sure. My problem is that VSCode recognizes it, and all is green. But when I try to run it with ts-node, it gives the error. |
The following worked for me: // project-root/src/types/express/index.d.ts
declare global {
namespace Express {
interface Request {
token?: string
}
}
} And then run
As ts-node does not use files because of slows startup time, we need to tell it explicitly. Hence '--files' is necessary here. |
VSCode gives me:
|
You need to import or export something to indicate that the file is a module. export {}; in that file, then the error should be gone. |
To help anyone who is just looking for something else to try here is what worked for me when trying to extend ExpressJS' Request. I had to have tried more than a dozen things before getting this to work:
"typeRoots": [
"./node_modules/@types",
"./your-custom-types-dir"
]
declare global {
namespace Express {
interface Request {
customBasicProperty: string,
customClassProperty: import("../path/to/CustomClass").default;
}
}
}
{
"restartable": "rs",
"ignore": [".git", "node_modules/**/node_modules"],
"verbose": true,
"exec": "ts-node --files",
"watch": ["src/"],
"env": {
"NODE_ENV": "development"
},
"ext": "js,json,ts"
} |
Worked for me. Thank you so much |
Works for me... I add "--files" flag in the run command "ts-node --files index.ts", and create express.d.ts with the follow content. declare namespace Express { |
An alternative to using the tsconfig.json {
"ts-node": {
"files": true
},
"compilerOptions":{
// ...
}
} |
@galletafromjell666 thanks bro! Worked for me :D |
That also worked for me, thanks! |
You are the man. To God honest I tried every single solution until I at last tried yours and it worked. Saved me from pulling all my hair. If I may, why did the other solutions not work? |
Saved my life, it works perfectly for me, Thank you |
I've tried this, this, etc
Example usage:
It does work if I compile directly (
tsc -p .
), it does work in Visual Code, but when I try to run withts-node
I always get:Any idea how can I make it work with ts-node?
Versions:
ts-node@7.0.1
typescript@3.0.1
https://stackoverflow.com/q/53765895/340760
The text was updated successfully, but these errors were encountered: