93

成功编译了我的 TypeScript 项目后,我打算在 VS Code 的调试模式下使用ts-node. 问题是,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

4

5 回答 5

138

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

我可以通过添加一个files和一个ts-node部分来解决它tsconfig.json

// tsconfig.json
{
  "ts-node": {
    "files": true
  },
  "files": [
    "src/index.ts",
    "src/global.d.ts"
  ],
  "compilerOptions":{
    //...
  }
}
于 2020-07-13T14:57:12.077 回答
110

从 7.0.0 中的 ts-node 开始,不会tsconfig.json在启动时加载文件。相反,你应该特别--files喜欢这样

ts-node --files src/boot.ts
于 2018-08-03T06:42:52.873 回答
16

TLDR

添加"ts-node": { "files": true },tsconfig.jsonts-node-dev预期工作

解释:

我正在ts-node-dev使用package.json

"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": {
     ...
  }
}
于 2021-07-22T16:31:18.160 回答
9

I spent way to much time on this issue tried almost everything like adding to typeRoots my typings folder, creating typing folder with structure typings/module/index.d.ts but nothing worked out so now I've figured it out now what the above answer meant

With a new version of ts-node I've changed for my project's scripts:

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

So your script will be changed to something like below

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

With the above in action your compile time increase a lot but I couldn't spend more time on this so I'm sticking to the above.

You also might like to visit https://github.com/TypeStrong/ts-node#help-my-types-are-missing.

于 2019-10-23T09:58:31.680 回答
9

这是我修复它的方法。将“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"
  },
于 2020-03-05T06:04:15.387 回答