【问题标题】:Typescript's declaration merging not working as expected using ts-nodeTypescript 的声明合并使用 ts-node 无法按预期工作
【发布时间】:2021-07-29 20:11:22
【问题描述】:

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

req.session.user = 123;

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

查看各种开源项目,例如HospitalRun components repository,我注意到他们在tsconfig.json 文件中的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>'

但是,当我将这段代码添加到用于改变会话对象的代码之上时,我不再遇到问题。不过,这似乎不是正确的方法。

此外,当我使用 tsc src/index.ts --build 而不是 ts-node src/index.ts 时,它也可以工作。

我在这里做错了什么?如何解决这个问题?我也尝试使用typeRoots,使用相同的文件夹。

【问题讨论】:

  • 您是否有指向演示此问题的 Web IDE 的链接,或您的问题的其他 minimal reproducible example?我怀疑问题是由于您的配置造成的,很难调试看不见的视线。
  • @jcalz 添加了我的示例!

标签: javascript node.js typescript express session


【解决方案1】:

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

使用ts-node运行typescript程序时,即使在tsconfig.json中指定了typeRoots,也无法识别自定义的.d.ts并提示Property 'x不存在类型y`错误。

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

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

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

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

旧:(2021 年 5 月 7 日)

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

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

如果您使用的是 VSCode,请尝试 Cmd + Shift + P 或 Ctrl + 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,
    },
}

【讨论】:

  • 你能举个例子吗?我真的不明白。
  • 从 tsconfig 中移除 include 部分并重启可视化代码或重启 TS 服务器
  • 不幸的是它没有工作,仍然是同样的错误。
  • 我做了一个最小的例子。并且可以成功识别类型。 github.com/michael-vascue/express-type 看看。尝试克隆它,看看你是否有同样的错误
  • 如果示例有效,您可以交叉检查差异。我遵循您提供的所有数据来创建它,所以唯一可能存在差异的是包的版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 2019-01-19
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多