admin管理员组文章数量:1331648
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 ponents 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"
],
"pilerOptions": {
"lib": [
"esnext",
"esnext.asynciterable"
],
"baseUrl": ".",
"skipLibCheck": true,
"module": "monjs",
"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.
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 ponents 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"
],
"pilerOptions": {
"lib": [
"esnext",
"esnext.asynciterable"
],
"baseUrl": ".",
"skipLibCheck": true,
"module": "monjs",
"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.
- Do you have a link to a web IDE that demonstrates this problem, or other minimal reproducible example of your issue? I suspect the issue is due to your configuration, which is hard to debug sight unseen. – jcalz Commented May 7, 2021 at 18:39
- @jcalz Added my example! – Bas Commented May 7, 2021 at 19:11
1 Answer
Reset to default 11LATEST 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./TypeStrong/ts-node/issues/1132#issuement-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": [...],
"pilerOptions": {
...
}
OLD: (07-MAY-2021)
There is no need to use include
in tsconfig.json
and the paths are not correct. The piler 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"
],
"pilerOptions": {
"lib": [
"esnext",
"esnext.asynciterable"
],
"baseUrl": ".",
"skipLibCheck": true,
"module": "monjs",
"esModuleInterop": true,
"target": "es6",
"moduleResolution": "node",
"outDir": "build",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"strictPropertyInitialization": false,
},
}
本文标签: javascriptTypescript39s declaration merging not working as expected using tsnodeStack Overflow
版权声明:本文标题:javascript - Typescript's declaration merging not working as expected using ts-node - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742266085a2443418.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论