For*_*eti 16 typescript typescript-typings ts-node
成功编译了我的TypeScript项目之后,我打算在VS Code的调试模式下运行它ts-node.问题是,ts-node找不到d.ts我创建的文件(虽然tsc它没有问题).
项目结构是:
/
conf/
dist/
src/
types/
package.json
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
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/**/*"
]
}
Run Code Online (Sandbox Code Playgroud)
定义文件ts-node找不到的是src/types/global.d.ts:
import { App } from '../App';
declare global {
namespace NodeJS {
interface Global {
app: App;
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以,尝试运行它ts-node我看到:
TSError: ? Unable to compile TypeScript:
src/boot.ts(15,59): error TS2339: Property 'app' does not exist on type 'Global'.
Run Code Online (Sandbox Code Playgroud)
如何全球解决?我发现这个/// <reference path="./types/global.d.ts" />诀窍,但我必须在每个使用的文件中重复它global.app.
我的TypeScript版本是3.0.1
djl*_*auk 94
我遇到了类似的问题,但我无法添加--files,因为我ts-node通过mocha(ie 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":{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
我希望有人会发现这很有用。
Sah*_*dhi 30
总长DR
添加"ts-node": { "files": true },fortsconfig.json以ts-node-dev按预期工作
解释:
我正在使用ts-node-dev类似package.json:
"scripts": {
"build": "tsc",
...
"dev": "ts-node-dev src/index.ts"
},
Run Code Online (Sandbox Code Playgroud)
npm run build工作正常但npm run dev失败,我的类型定义文件位于src/types/*.
在我添加以下内容后,它开始正常工作tsconfig.json
{
"ts-node": { "files": true }, // add this
"compilerOptions": {
...
}
}
Run Code Online (Sandbox Code Playgroud)
小智 28
ts-node --files src/boot.ts
在7.0.0中的ts-node,默认不tsconfig.json启动时加载文件,你应该具体--files
小智 8
这是我修复它的方法。将“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"
},
Run Code Online (Sandbox Code Playgroud)
我在这个问题上花了很多时间尝试了几乎所有的事情,比如添加到 typeRoots 我的typings文件夹,创建带有结构typings/module/index.d.ts的typing文件夹,但没有任何效果,所以现在我已经弄清楚了上面的内容回答的意思
使用新版本的 ts-node,我已经更改了我的项目脚本:
ts-node@6: ts-node src/index.ts
ts-node@7: ts-node --files src/index
Run Code Online (Sandbox Code Playgroud)
所以你的脚本将更改为如下所示
"scripts": {
"dev": "nodemon --exec ts-node --files src/index",
}
Run Code Online (Sandbox Code Playgroud)
执行上述操作后,您的编译时间会增加很多,但我不能花更多时间在这上面,所以我坚持上述操作。
您可能还想访问https://github.com/TypeStrong/ts-node#help-my-types-are-missing。
| 归档时间: |
|
| 查看次数: |
4553 次 |
| 最近记录: |