ts-node 找不到我的类型定义文件

2024-01-27

当我跑步时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#missing-types https://github.com/TypeStrong/ts-node#missing-types按照它的建议,我修改了我的 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 https://stackoverflow.com/a/43523944/61624


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


启用 ts 节点--files选项中tsconfig.json像这样:

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

进一步阅读:

  • 帮助!我的类型丢失了! https://github.com/TypeStrong/ts-node/issues/866
  • ts-node 忽略 d.ts 文件,而 tsc 成功编译项目 https://stackoverflow.com/a/62878548/8791436
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ts-node 找不到我的类型定义文件 的相关文章

随机推荐