【问题标题】:Where should I put the index.d.ts file?我应该把 index.d.ts 文件放在哪里?
【发布时间】:2019-10-04 09:26:29
【问题描述】:

我正在编写一个 nodeJS 服务,它使用一堆没有 @types 的 npm 模块。

tsc 错误消息告诉我需要添加 index.d.ts 文件,但它没有告诉我将它放在哪里。我的 helper.spec.ts 文件也导入了相同的模块,在 jest 运行时也无法检测到 index.d.ts

我将该文件与 tsconfig.json 一起放在我的根目录中,但它没有检测到它。我的文件和结构如下所示:

文件夹结构

node_modules
build
    app.js
    helper.js
    another.js
spec
    - helper.spec.ts
    - another.spec.ts
src
    - app.ts
    - helper.ts
    - another.ts
tsconfig.json
index.d.ts
jest.config.json
package.json
package-lock.json

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "allowJs": true,                       /* Allow javascript files to be compiled. */
    "outDir": "build",                        /* Redirect output structure to the directory. */
    "strict": true,                           /* Enable all strict type-checking options. */
  },
  "include": [
    "src/**/*.ts",
  ],
  "exclude": [
      "node_modules",
      "**/*.spec.ts"
  ]
}

index.d.ts

declare module "module-one";
declare module "module-two";
declare module "module-three";

package.json

{
  "dependencies": {
    "module-one": "^2.0.4",
    "module-two": "^1.3.3",
    "module-three": "0.0.3",
    "@types/lodash": "^4.14.129",
  },
  "devDependencies": {
    "@types/jest": "^24.0.13",
    "@types/node": "^9.6.0",
    "cpx": "^1.5.0",
    "jest": "^24.8.0",
    "ts-jest": "^24.0.2",
    "typescript": "^3.4.5"
  },
  "scripts": {
    "start": "cd build && node app.js",
    "test": "jest",
    "build": "tsc",
    "postinstall": "npm run-script build"
  },
}

tsc 和 jest 期望 index.d.ts 在哪里?

一些文章建议为每个模块创建一个 index.d.ts,例如./types/module-one/index.d.ts./types/module-two/index.d.ts./types/module-three/index.d.ts 然后编辑 tsconfig.json compilerOptions.typeRoots 以包含 ./types 文件夹。

但我只想拥有 1 个包含所有声明的 index.d.ts。

当我编辑 tsconfig.json include 以包含 index.d.ts 文件时,我发现 tsc 可以编译我的 src 文件夹中的文件。但是,当我运行 jest 时,它仍然抱怨我的模块 index.d.ts 丢失。

编辑: 如果我删除了我的 tsconfig.json,那么 jest 将正常运行而不会抱怨缺少模块,但是我无法 tsc 构建我的 src 文件。

如果我保留 tsconfig.json,那么 tsc 将构建我的 src 文件,但 jest 会抱怨 module-one 未定义。

编辑 2: 我发现如果我设置[jest.config.ts].globals.ts-jest.diagnostics = false,那么错误就会消失并且我的所有测试都通过了!但我认为这不是正确的解决方法?

【问题讨论】:

标签: typescript jestjs tsc ts-jest


【解决方案1】:

TLDR:出于正常人类无法理解的原因,TypeScript 在 baseUrl 编译器选项指向的目录中查找 index.d.ts!

愚蠢但正确的答案是“你要把你的 index.d.ts 放在你编译器能找到的地方”

嗯,没那么白痴。请记住,从一个工作项目中复制粘贴一半的配置,从另一个项目中复制粘贴一半,很可能会给您提供非工作配置。

根据this article,我已经添加到我的tsconfig.json

{
    "compilerOptions": {
        "typeRoots": [ "./types", "./node_modules/@types"],
        ...
    },
    ...
}

哎呀,什么都没发生。

很可能,因为“此外,本文假设您使用的是 TypeScript 2.x。” - 我正在使用 3.x。

愚蠢的复制粘贴永远无法正常工作。

使用 truss(与 FreeBSD 对应的 strace),我发现转译器完全忽略了我的 typeRoots。它在 /node_modules/@types/vue-native-notification/index.d.ts 中搜索了我的 index.d.ts - 在我的 src 目录中,在我的项目目录中,在我的项目目录中,直到 /node_modules/@types/ vue-native-notification/index.d.ts

好的,将我的 src/types/vue-native-notification 符号链接到 node_modules/@types/vue-native-notification 就可以了,但这不是我想要的。我不想根据转译器的默认偏好来修补我的树!

根据official docs,typesRoot 选项已重命名为 types。非常微软化。

好的,我已将 typesRoot 更改为 types。

现在我无法跟踪读取 src/types/vue-native-notification/index.d.ts ... 的编译器并抛出相同的错误!

我在 index.d.ts 中写了一些词,发现它没有被编译。确实很奇怪。

没有一丝希望,我尝试了setting a baseUrl 看似毫无意义的想法,但它有所帮助。删除类型编译器选项并没有改变任何事情。

我对成功完全不满意,它看起来很神奇。但它有效。

【讨论】:

  • official docs不要typeRoots改名为types;他们说“此功能与 typeRoots 的不同之处在于它只指定您想要包含的确切类型,而 typeRoots 支持说您想要特定的文件夹。”
猜你喜欢
  • 2010-12-01
  • 2018-09-28
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 2016-05-23
  • 1970-01-01
  • 2016-08-25
  • 2013-08-15
相关资源
最近更新 更多