ts-node ignores d.ts files while tsc successfully compiles the project

懵懂的女人 提交于 2019-12-09 14:19:52

问题


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).

Project structure is:

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

tsconfig.json relevant entries are:

{
    "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/**/*"
    ]
}

The definition file ts-node can't find is 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'.

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.

My TypeScript version is 3.0.1


回答1:


ts-node --files src/boot.ts

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




回答2:


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

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.



来源:https://stackoverflow.com/questions/51610583/ts-node-ignores-d-ts-files-while-tsc-successfully-compiles-the-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!