我应该把 index.d.ts 文件放在哪里?

Jak*_*ake 8 typescript tsc jestjs ts-jest

我正在编写一个 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
Run Code Online (Sandbox Code Playgroud)

配置文件

{
  "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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

索引.d.ts

declare module "module-one";
declare module "module-two";
declare module "module-three";
Run Code Online (Sandbox Code Playgroud)

包.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"
  },
}
Run Code Online (Sandbox Code Playgroud)

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.jsoncompilerOptions.typeRoots以包含./types文件夹。

但我只想有 1 个 index.d.ts 和所有声明。

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

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

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

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

Ale*_*sky 9

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

白痴但正确的答案是“你要把你的index.d.ts放在你的编译器会找到它的地方”

好吧,没那么白痴。请记住,复制粘贴一个工作项目中的一半配置和其他项目中的一半很可能会为您提供无法工作的配置。

根据这篇文章,我已添加到我的 tsconfig.json

{
    "compilerOptions": {
        "typeRoots": [ "./types", "./node_modules/@types"],
        ...
    },
    ...
}
Run Code Online (Sandbox Code Playgroud)

哎呀,什么也没发生。

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

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

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

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

根据官方文档,typesRoot 选项已重命名为 types。非常微软化。

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

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

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

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

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

  • [官方文档](https://www.typescriptlang.org/tsconfig#types)_不_说“typeRoots”已重命名为“types”;他们说“此功能与 typeRoots 的不同之处在于,它仅指定您想要包含的确切类型,而 typeRoots 支持说您想要特定的文件夹。” (2认同)