尽管添加了.d.ts文件,但类型声明错误

时间:2020-08-31 16:14:05

标签: angular typescript

我正在使用一个名为update-immutable的npm软件包,该软件包恰好没有@types内置的或由noImplicitAny内置的打字稿定义。因此,我在项目中创建了一个类型定义文件,现在VS Code检测到此程序包的正确类型。

但是,如果Could not find a declaration file for module 'update-immutable'. '.../node_modules/update-immutable/dist/update.js' implicitly has an 'any' type.选项设置为true,Angular的编译器似乎无法检测到该类型定义文件,并且会抛出错误.d.ts

如何让Angular的编译器找到我为正在使用的npm包创建的traceResolution类型定义?

我确实通过启用update-immutable.d.ts发现,如果将projectroot + src | + app | | + states | | + core | | + core.reducer.ts (where update-immutable is imported) | + types | + update-immutable | + index.d.ts (type file created by me) + node_modules | + update-immutable | + ... + tsconfig.json 文件直接放在项目根目录下,它将检测到它。但是我想尽可能地保持这些类型的文件井井有条,不要在项目根目录下乱扔垃圾。

项目树:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "downlevelIteration": true,
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "jszip": [
        "node_modules/jszip/dist/jszip.min.js"
      ]
    },
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

tsconfig.json:

index.d.ts

declare module 'update-immutable' { export default function update<T>(view: T, upd: object): T; } 类型定义:

NSImage

1 个答案:

答案 0 :(得分:0)

我没有使用 Angular,但在另一个项目中遇到了这个问题。

我的问题是我需要在 paths 文件中同时设置 typeRootstsconfig.json。这是我从 andyperlitch/typeroots-fail 中引用的示例:

// tsconfig.json
{
  "baseUrl": ".",
  "typeRoots": ["./node_modules/@types", "./custom-types"],
  "paths": {
    "*": ["*", "./custom-types/*"]
  }
}

看到一个 larger discussion on GitHub