问题描述
我已经使用create react app typescript创建了一个项目.我有一些d.ts文件,其中定义了接口类型和枚举.当我运行启动脚本时.它无法加载d.ts文件.
I have created a project using create react app typescript. I have a few d.ts files where I have defined interfaces types and enums. When I run start script. It is not able to load the d.ts files.
以下是我的tsconfig文件.
following is my tsconfig file.
{
"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/*"
]
}
typeRoot指向src/*,其中我有d.ts文件,但没有d.ts加载.我得到以下错误:
typeRoot points to src/* , where i have my d.ts files but none of the d.ts is loaded. and I get following error:
类型错误:找不到名称"IAlarmsDetails". TS2304
Type error: Cannot find name 'IAlarmsDetails'. TS2304
interface IAlarmProps {
alarm: IAlarmsDetails;
}
这是Alarm.d.ts之一中IAlarmsDetails的声明
This is the declaration for IAlarmsDetails in one of Alarm.d.ts
declare type IAlarmsList = IAlarmsDetails[];
请让我知道我在这里想念的东西.我不想使用弹出选项并编写自己的构建配置.
Please let me know what I'm missing here.I do not want to use eject option and write my own build config.
推荐答案
似乎在create-react-app(版本3.0.1)中直接使用.d.ts
文件的唯一方法是为其命名global.d.ts
并将其放在src
目录中:
It seems that the only way to use a .d.ts
file out of the box with create-react-app (version 3.0.1) is to name it global.d.ts
and put it in the src
directory:
src/global.d.ts
我不确定为什么没有在任何地方记录此规则,但这是我能够使它生效的唯一方法.
I'm not sure why this rule isn't documented anywhere but that was the only way I was able to get it to work.
这篇关于创建React App Typescript不会加载d.ts文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!