javascript - 使用 ts-node 的 Typescript 声明合并无法按预期工作

对于使用 express-session 包的项目,我试图通过简单地添加用户 key 来改变 session 对象。

req.session.user = 123;

来自this question's 接受的答案,我知道我可以使用声明合并来扩展 SessionData 接口(interface),使用我自己的接口(interface)。

查看各种开源项目,例如 HospitalRun components repository我注意到他们在 include 部分下的 tsconfig.json 文件中有 types 目录,如下所示。

  "include": [
    "src",
    "types"
  ]

我的整个 tsconfig.json 看起来像这样,它位于项目的根目录中。

{
    "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,
    },
}

我尝试做同样的事情,在这个文件夹的根目录(~/types/)中有一个名为 express-session.d.ts 的文件,具有以下内容内容:

import session from 'express-session';

declare module 'express-session' {
    interface SessionData {
        user: any;
    }
} 

但是,我一直收到的错误是这样的。

 Property 'user' does not exist on type 'Session & Partial<SessionData>'

然而,当我将这段代码添加到我用于改变我的 session 对象的代码之上时,我就不再有问题了。不过,这似乎不是正确的方法。

此外,当我使用 tsc src/index.ts --build 而不是 ts-node src/index.ts 时,它也有效。

我在这里做错了什么?这怎么能解决?我还尝试使用 typeRoots,使用相同的文件夹。

最佳答案

最新更新(2021 年 5 月 8 日)

使用ts-node运行typescript程序时,即使在tsconfig.json中指定了typeRoots,也无法识别自定义的.d.ts和提示符Property 'x does not exist on type y` 错误。

根据 https://github.com/TypeStrong/ts-node/issues/1132#issuecomment-716642560

ts-node的贡献者之一提出了多种解决方法。

这是其中之一: 在tsconfig.json中指定file: true标志通知ts-node加载files,include exclude 启动时 tsconfig.json 中的选项

{
  "ts-node": {
    "files": true
  },
  "exclude": [...],
  "compilerOptions": {
   ...
}

旧:(2021 年 5 月 7 日)

tsconfig.json 中不需要使用include,路径不正确。编译器可以在目录和子目录下搜索ts文件

尝试删除它。并重启TS服务器。

如果您使用的是 VSCode,请尝试 Cmd + Shift + PCtrl + Shift + P 搜索Restart TS server 看用户输入错误是否还存在

{
    "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,
    },
}

https://stackoverflow.com/questions/67436289/

相关文章:

r - 将列中以冒号分隔的字符串拆分为 R 中的不同列

fortran - Fortran 中的嵌套名单

regex - 如何将 RegEx token 传递给 RegEx 替换中的 PowerShell

amazon-web-services - CloudFormation YAML - 带有条件语句

reactjs - 创建一个可以通过函数调用显示的 React 组件(如 react-toastif

visual-studio-code - 删除 VSCode 中的 Sublime 文本主题

python - 将 VSCode 更新到 1.56.1 后,出现错误 : "Cannot acti

r - 使用数值条件对一系列列进行编码

python - 有没有办法缩短多个 if 语句?

python - 基于字典值映射Python列表