For a project using the express-session
package, I’m trying to mutate the session
object by simply adding a user key.
req.session.user = 123;
Coming from this question’s accepted answer, I understand I could use declaration merging to extend the SessionData
interface, using my own interface.
Looking at various open-source projects, such as the HospitalRun components repository I notice them having the types
directory in their tsconfig.json
file under the include
section like this.
"include": [
"src",
"types"
]
My whole tsconfig.json
looks like this, which lives in the root of the project.
{
"include": [
"types",
"src",
],
"exclude": [
"node_modules"
],
"compilerOptions": {
"lib": [
"esnext",
"esnext.asynciterable"
],
"baseUrl": ".",
"skipLibCheck": true,
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"moduleResolution": "node",
"outDir": "build",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"strictPropertyInitialization": false,
},
}
I tried doing the same, together a file called express-session.d.ts
in the root of this folder (~/types/
), having the following contents:
import session from 'express-session';
declare module 'express-session' {
interface SessionData {
user: any;
}
}
However, the error I keep receiving is this.
Property 'user' does not exist on type 'Session & Partial<SessionData>'
When I do however add this piece of code above the code I use for mutating my session object, I no longer have the problem. This doesn’t seem like the right approach though.
Also, when I use tsc src/index.ts --build
instead of ts-node src/index.ts
it also works.
What am I doing wrong here? How can this be fixed? I also tried using the typeRoots
, using the same folder.
Advertisement
Answer
LATEST UPDATE (08-MAY-2021)
When running the typescript program by using ts-node
, even typeRoots
are specified in tsconfig.json, it cannot recognise the custom .d.ts and prompt Property 'x
does not exist on type y` error.
According to https://github.com/TypeStrong/ts-node/issues/1132#issuecomment-716642560
One of the contributors of ts-node
suggested multiple ways to solve it.
Here is one of it:
Specifying file: true
flag in tsconfig.json
to inform ts-node
to load files
, include
and exclude
options from tsconfig.json
on startup
{
"ts-node": {
"files": true
},
"exclude": [ ],
"compilerOptions": {
}
OLD: (07-MAY-2021)
There is no need to use include
in tsconfig.json
and the paths are not correct. The compiler can search the ts file in the directory and sub-directories
Try to remove it. and restart TS server.
If you are using VSCode, try Cmd + Shift + P or Ctrl + Shift + P and search Restart TS server
and see if the user type error still exist
{
"exclude": [
"node_modules"
],
"compilerOptions": {
"lib": [
"esnext",
"esnext.asynciterable"
],
"baseUrl": ".",
"skipLibCheck": true,
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"moduleResolution": "node",
"outDir": "build",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"strictPropertyInitialization": false,
},
}