Ts-node 忽略 d.ts 文件,而 tsc 成功编译项目

在成功编译了 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;
}
}
}

So, trying to run it with ts-node I see:

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在每个文件中重复它。

My TypeScript version is 3.0.1

38555 次浏览

从7.0.0中的 ts-node 开始,在启动时不从 tsconfig.json加载文件

ts-node --files src/boot.ts

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

在新版本的 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#missing-types

下面是我是如何修复它的: 在开发脚本中添加“ noemon —— 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"
},

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

我可以通过在 tsconfig.json上增加一个 files和一个 ts-node节来解决这个问题,就像这样:

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

TLDR

Add "ts-node": { "files": true }, in tsconfig.json for ts-node-dev to work as expected

解释:

I was using ts-node-dev in package.json like:

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

npm run build was working fine but npm run dev was failing, My type definition files are in src/types/*.

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

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

而其他的解决方案都很好。在 https://www.npmjs.com/package/ts-node#missing-types中可以找到 ts-node 建议的方法(使用—— file 除外,根据 ts-node 这是最后使用的方法)。

It recommends 3 different types based on your use case and preferences.

  1. 在 tsconfig.json 中使用 developerOptions.typeRoots

对于全局定义,可以使用 typeRoots 编译器选项。这要求将类型定义结构化为类型包(而不是松散的 TypeScript 定义文件)。更多关于此工作原理的详细信息可以在 TypeScript 手册中找到

{
"compilerOptions": {
"typeRoots" : ["./node_modules/@types", "./typings"]
}
}
  1. 在 tsconfig.json 中使用路径

对于模块定义,可以使用路径

{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"custom-module-type": ["types/custom-module-type"]
}
}
}
  1. 使用三斜杠指令

另一个选项是三斜杠指令。如果您不希望更改编译器选项或者为 typeRoots 构造类型定义,这可能会有所帮助。下面是一个三斜杠指令作为项目中的相对路径的示例:

/// <reference path="./types/lib_greeter" />
import {Greeter} from "lib_greeter"
const g = new Greeter();
g.sayHello();