使用 ts-node 合并打字稿的声明无法按预期工作 [英] Typescript's declaration merging not working as expected using ts-node

查看:74
本文介绍了使用 ts-node 合并打字稿的声明无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

来自 这个问题的 已接受答案,我知道我可以使用声明合并来扩展 SessionData界面,使用我自己的界面.

Coming from this question's accepted answer, I understand I could use declaration merging to extend the SessionData interface, using my own interface.

查看各种开源项目,例如HospitalRun 组件存储库注意他们在 include 部分下的 tsconfig.json 文件中有 types 目录,就像这样.

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"
  ]

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

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

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

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.

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

Also, when I use tsc src/index.ts --build instead of ts-node src/index.ts it also works.

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

What am I doing wrong here? How can this be fixed? I also tried using the typeRoots, using the same folder.

推荐答案

LATEST UPDATE (08-MAY-2021)

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

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.

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

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

One of the contributors of ts-node suggested multiple ways to solve it.

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

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": {
   ...
}


旧:(2021 年 5 月 7 日)

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

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

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

Try to remove it. and restart TS server.

如果您使用 VSCode,请尝试 Cmd + Shift + PCtrl + Shift + P 搜索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,
    },
}

这篇关于使用 ts-node 合并打字稿的声明无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆