7

我有一个使用 CRA 打字稿创建的项目。

在项目的根目录下存在tsconfig.json一个名为types. 在 types 文件夹中,我modulename.d.ts为在 node_modules 处没有默认类型的模块创建了一个文件,并且在@types\module-name.

但我在编译时得到错误。

类型错误:找不到模块“名称”的声明文件。'/*/node_modules/thename/dist/entry.js' 隐含了一个 'any' 类型。

为什么编译器在类型文件夹中找不到类型声明?

// .d.ts
declare module 'foo-react' {
    import * as React from 'react';
    // ...
}

这是tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": { "*": ["types/*"] },
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "plugins": [{ "name": "typescript-tslint-plugin" }],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve"
  },
  "include": ["src"]
}
4

1 回答 1

4

原来如此

  1. 我将自定义类型文件夹重命名为@types
  2. 文件夹的路径@types(我在项目根目录下的自定义路径)应该添加到types属性而不是paths属性。 "types":["./@types"]
  3. @types具有类似@types/<module_name>/index.d.ts或结构的文件夹中@types/moduleName.d.ts。现在该文件可以将该模块声明为any或详细声明所有类型。

Typescript 手册中类型编译器选项的定义

--types string[] 要包含的类型定义的名称列表。有关更多详细信息,请参阅@types、—typeRoots 和—types

于 2019-06-26T11:12:56.773 回答