创建React App Typescript不会加载d.ts文件

Anu*_*rma 2 typescript tsconfig create-react-app .d.ts

我已经使用create react app typescript创建了一个项目。我有一些d.ts文件,其中定义了接口类型和枚举。当我运行启动脚本时。它无法加载d.ts文件。

以下是我的tsconfig文件。

{
  "compilerOptions": {
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "es2017"
    ],
    "allowJs": true,
    "skipLibCheck": false,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve",
    "pretty": true,
    "preserveConstEnums": true,
    "typeRoots": [
      "./node_modules/@types",
      "src/*"
    ]
  },
  "include": [
    "src/*"
  ]
}
Run Code Online (Sandbox Code Playgroud)

typeRoot指向src / *,其中我有d.ts文件,但没有d.ts加载。我得到以下错误:

键入错误:找不到名称“ IAlarmsDetails”。TS2304

interface IAlarmProps {
        alarm: IAlarmsDetails;
}       
Run Code Online (Sandbox Code Playgroud)

这是Alarm.d.ts之一中IAlarmsDetails的声明。

declare type IAlarmsList = IAlarmsDetails[];
Run Code Online (Sandbox Code Playgroud)

请让我知道我在这里想念的东西。我不想使用弹出选项并编写自己的构建配置。

Mat*_*wne 5

似乎将.d.ts文件与create-react-app(版本3.0.1)一起使用的唯一方法是将其命名global.d.ts并放在src目录中:

src/global.d.ts

我不确定为什么没有在任何地方记录此规则,但这是我能够使它生效的唯一方法。

  • `global.d.ts` 是 TypeScript 手册中示例中使用的常见约定。我很幸运地发现了这一点,因为它在我们的一个存储库中工作(巧合的是它有一个“global.d.ts”),但在另一个我试图使用不同名称的文件进行某些声明的存储库中却没有发现这一点。也许 create-react-app 的创建者没有意识到这只是一个约定,而不是所有全局类型声明必须去的地方......只是猜测。 (2认同)