【问题标题】:ts-node can't find my type definition filests-node 找不到我的类型定义文件
【发布时间】:2020-07-02 01:36:26
【问题描述】:

当我运行 ts-node node_modules/jasmine/bin/jasmine 时,我收到以下错误:

tsc/globals.ts:7:12 - error TS2304: Cannot find name 'SugarcubeState'.

7     State: SugarcubeState;
             ~~~~~~~~~~~~~~

这是那个全局文件:

/* eslint-disable @typescript-eslint/no-explicit-any */
console.log("global.ts");

// eslint-disable-next-line @typescript-eslint/no-namespace
declare namespace NodeJS {
  interface Global {
    State: SugarcubeState;
    setup: {};
  }
}

declare const State: SugarcubeState = {
  variables: {}
};

declare const setup: any = {
  variables: {}
};

这是我的 index.d.ts:

type SugarcubeVariables = {
};

type SugarcubeState = { variables: SugarcubeVariables };

它们都在同一个目录中,Visual Studio 代码不会抱怨任何事情。为什么 ts-node 似乎找不到我的类型定义文件?

我在谷歌上搜索并找到了这个网站:https://github.com/TypeStrong/ts-node#help-my-types-are-missing 按照它的建议,我修改了我的 tsconfig 文件以拥有一个

"typeRoots": ["tsc"],                       /* List of folders to include type definitions from. */

在其中,但它对错误没有影响。我也试过这个:

    "types": ["tsc/index.d.ts"],                           /* Type declaration files to be included in compilation. */

但同样,我收到的错误没有区别。如何让 ts-node 识别我的 .d.ts 文件?

PS:如果您想知道我为什么要这样定义事物,请参阅此答案 https://stackoverflow.com/a/43523944/61624


我重读了那个链接,看来我需要一个非常具体的目录结构。问题是,它说我只需要这个目录结构中的模块名称,并且考虑到我编写 index.d.ts 的方式,我不知道如何命名这个目录。

【问题讨论】:

标签: node.js typescript ts-node


【解决方案1】:

tsconfig.json 中启用 ts-node --files 选项,如下所示:

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

进一步阅读

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-24
    • 2018-07-20
    • 2019-09-22
    • 2018-06-05
    • 2021-08-15
    • 2017-06-18
    • 2018-07-31
    • 1970-01-01
    相关资源
    最近更新 更多