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

标签 typescript jestjs tsc 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

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"
  ]
}

索引.d.ts
declare module "module-one";
declare module "module-two";
declare module "module-three";

包.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 ,然后错误消失,我的所有测试都通过了!但我不认为这是正确的解决办法?

最佳答案

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 符号链接(symbolic link)到 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 ,但它有帮助。删除类型编译器选项并没有改变任何事情。
我对成功完全不满意,它看起来很神奇。但它有效。

关于typescript - 我应该把 index.d.ts 文件放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56179761/

相关文章:

typescript - tsc 不排除 node_modules

angular - 在组件中使用 NgbModule.forRoot() 导致测试失败

javascript - TypeScript - 结合两个接口(interface)并使其某些成员成为强制性的

reactjs - React 测试库和 Jest 中的模拟路由器

java - 从 Java 运行 TypeScript 编译器

typescript - npm 不更新 tsc 版本

javascript - Ngrx store dispatch 不接受字符串作为参数

扩展另一个内部类的 TypeScript 内部类不可分配给父类(super class)(声明合并)

javascript - 如何正确模拟节点模块以进行 Jest 单元测试

reactjs - 如何模拟和测试 MaterialUI - makeStyles