代码之家  ›  专栏  ›  技术社区  ›  Forseti

ts节点在tsc成功编译项目时忽略d.ts文件

  •  6
  • Forseti  · 技术社区  · 6 年前

    在成功编译了TypeScript项目之后,我打算使用 ts-node . 问题是, ts节点 找不到 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节点 找不到是 src/types/global.d.ts :

    import { App } from '../App';
    
    declare global {
        namespace NodeJS {
            interface Global {
                app: App;
            }
        }
    }
    

    所以,试着用 ts节点 我懂了:

    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

    1 回复  |  直到 6 年前
        1
  •  24
  •   欧阳斌    6 年前

    ts-node --files src/boot.ts

    7.0.0中的ts节点,默认情况下不从 tsconfig.json 启动时,应该指定--files

        2
  •  0
  •   Black Mamba    5 年前

    我花了很多时间在这个问题上,尝试了几乎所有的方法,比如在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
    

    随着上面的操作,你的编译时间增加了很多,但我不能花更多的时间在这上面,所以我坚持以上。

    你也可以去看看 https://github.com/TypeStrong/ts-node#help-my-types-are-missing .

    推荐文章