【问题标题】:ts-node ignores d.ts files while tsc successfully compiles the projectts-node 在 tsc 成功编译项目时忽略 d.ts 文件
【发布时间】:2019-01-07 16:26:21
【问题描述】:

成功编译我的 TypeScript 项目后,我打算使用 ts-node 在 VS Code 的调试模式下运行它。问题是,ts-node 找不到我创建的 d.ts 文件(而 tsc 没有问题)。

项目结构为:

/
    conf/
    dist/
    src/
        types/
package.json
tsconfig.json

tsconfig.json相关条目为:

{
    "compilerOptions": {
        "target": "es2017",
        "module": "commonjs",
        // "lib": [],
        "sourceMap": true,
        "outDir": "dist",
        "rootDir": "src",
        "moduleResolution": "node",
        "baseUrl": ".",
        "paths": {
            "*": [
                "node_modules/*",
                "src/types/*"
            ]
        },
        // "rootDirs": [],
        // "typeRoots": [],
        // "types": [],
    },
    "include": [
        "src/**/*"
    ]
}

定义文件ts-node找不到是src/types/global.d.ts

import { App } from '../App';

declare global {
    namespace NodeJS {
        interface Global {
            app: App;
        }
    }
}

所以,尝试使用 ts-node 运行它,我明白了:

TSError: ⨯ Unable to compile TypeScript:
src/boot.ts(15,59): error TS2339: Property 'app' does not exist on type 'Global'.

如何全局解决?我发现 /// <reference path="./types/global.d.ts" /> 可以解决问题,但我必须在每个文件中使用 global.app 重复它。

我的 TypeScript 版本是 3.0.1

【问题讨论】:

    标签: typescript typescript-typings ts-node


    【解决方案1】:

    我遇到了类似的问题,但我无法添加--files,因为我通过mocha(即mocha -r ts-node/register ...)注册模块来运行ts-node

    我可以通过将filests-node 部分添加到tsconfig.json 来解决它,如下所示:

    // tsconfig.json
    {
      "ts-node": {
        "files": true
      },
      "files": [
        "src/index.ts",
        "src/global.d.ts"
      ],
      "compilerOptions":{
        //...
      }
    }
    

    【讨论】:

    • 这仅适用于我使用 "ts-node" 部分。谢谢。
    • 可以确认只添加ts-node 部分会使mocha 再次编译而不会破坏tsc,谢谢@Sharpiro!
    • 你是我的英雄!谢谢!
    • 非常感谢,这可能会花费我几个小时。
    • 几个小时的谷歌搜索,这个 ts-node 块就是答案。重写了我的整个启动脚本
    【解决方案2】:

    从 7.0.0 中的 ts-node 开始,在启动时不会从 tsconfig.json 加载文件。相反,您应该像这样指定--files

    ts-node --files src/boot.ts
    

    【讨论】:

    • 是的,--files 成功了。但是我不知道为什么typeRoots 不知道。根据github.com/TypeStrong/ts-node#help-my-types-are-missing,这是要走的路,但我试过了,但没有帮助。无论如何,--files 有效,但我必须找到一种方法将其连接到 VS Code 的launch.json。最后,我在项目根目录中创建了一个ts-node.js 文件,该文件仅包含:require('ts-node').register({ files: true }),并在launch.json"runtimeArgs": [ "-r", "./ts-node.js" ] 中引用了它。非常感谢您的帮助!
    • 这只是节省了数小时的类型声明文件、环境变量等。非常感谢@Forseti!
    • 每次遇到这个问题,我总是会再次找到这条评论,它立即解决了问题。我很高兴这条评论会一直在我身边。
    • 这里是--files 选项的解释,虽然我仍然不明白它为什么起作用:github.com/TypeStrong/ts-node/issues/…
    • 非常感谢。我仍然会永远质疑为什么 TS-Node 不会自动执行此操作,它只会让生活变得更艰难。实际上应该有一个选项来禁用它并默认启用它,而不是相反。
    【解决方案3】:

    TLDR

    tsconfig.json 中添加"ts-node": { "files": true }, 以使ts-node-dev 按预期工作

    解释:

    我在package.json 中使用ts-node-dev,例如:

    "scripts": {
        "build": "tsc",
        ...
        "dev": "ts-node-dev src/index.ts"
      },
    

    npm run build 工作正常,但 npm run dev 失败,我的类型定义文件位于 src/types/*

    在我的tsconfig.json 中添加以下内容后,它开始正常工作

    {
      "ts-node": {  "files": true }, // add this
      "compilerOptions": {
         ...
      }
    }
    

    【讨论】:

      【解决方案4】:

      这是我修复它的方法。将“nodemon --exec ts-node --files src/app.ts”添加到您的开发脚本中。

       "scripts": {
          "start": "node dist/app.js",
          "dev": "nodemon --exec ts-node --files src/app.ts",
          "build": "tsc -p",
          "test": "echo \"Error: no test specified\" && exit 1"
        },
      

      【讨论】:

        【解决方案5】:

        我在这个问题上花了很多时间尝试了几乎所有的事情,比如将我的类型文件夹添加到 typeRoots,创建具有结构 typings/module/index.d.ts 的类型文件夹,但没有任何结果,所以现在我已经弄清楚了上面的答案是什么意思

        使用新版本的 ts-node 我已更改项目脚本:

        ts-node@6: ts-node src/index.ts
        ts-node@7: ts-node --files src/index
        

        因此您的脚本将更改为如下所示

        "scripts": {
            "dev": "nodemon --exec ts-node --files src/index",
          }
        

        在执行上述操作后,您的编译时间会增加很多,但我不能花更多时间在这上面,所以我坚持上述操作。

        您也可以访问https://github.com/TypeStrong/ts-node#help-my-types-are-missing

        【讨论】:

        • 您发布的链接是真正的解决方案(不会减慢编译时间):使用 typeRoots 编译器选项。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-25
        • 2018-12-29
        • 1970-01-01
        • 2016-06-02
        • 2019-09-22
        • 2017-01-27
        • 1970-01-01
        相关资源
        最近更新 更多