在tsc成功编译项目时,ts-node会忽略d.ts文件 [英] ts-node ignores d.ts files while tsc successfully compiles the project

查看:418
本文介绍了在tsc成功编译项目时,ts-node会忽略d.ts文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Having compiled my TypeScript project successfully, I intended to run it in VS Code's debug mode using ts-node. Problem is, ts-node can't find d.ts files I created (while tsc has no problem with it).

项目结构为:

/
    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-nodesrc/types/global.d.ts:

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

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

因此,尝试使用ts-node运行它,我看到了:

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在每个文件中重复进行此操作.

How to resolve it globally? I've found that /// <reference path="./types/global.d.ts" /> does the trick but I'd have to repeat it in every file using global.app.

我的TypeScript版本是3.0.1

My TypeScript version is 3.0.1

推荐答案

ts-node --files src/boot.ts

ts-node在7.0.0中,默认情况下不要在启动时从tsconfig.json加载文件,您应指定--files

ts-node in 7.0.0, default do not Load files from tsconfig.json on startup, you should specific --files

这篇关于在tsc成功编译项目时,ts-node会忽略d.ts文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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