reactjs - 创建 React 应用程序和全局类型未被拾取
问题描述
我有两个全局类型文件emotion.d.ts 和global.d.ts,它们不再被拾取。
目前使用情感主题并在emotion.d.ts 中创建了一个全局类型文件,看起来像这样:
import "@emotion/react";
declare module "@emotion/react" {
export interface Theme {
platform: PlatformType;
colors: {
D1: string;
D2: string;
D3: string;
D4: string;
D5: string;
};
categoryColors: {
GreenGradient: string;
BlueGradient: string;
PurpleGradient: string;
};
}
}
一切正常,但是当我扩展 Window 时,我不得不使用一些全局类型。所以我创建了如下所示的 global.d.ts 文件:
declare interface Window {
Intercom: (
action: string,
payload?: { [key: string]: string | number | boolean },
) => void;
}
declare interface Document {
documentMode?: number;
}
但是,现在当我运行该应用程序时,它找不到任何类型。我的 TS 配置如下所示:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"noFallthroughCasesInSwitch": true,
"typeRoots": ["./src/global.d.ts", "./src/emotion.d.ts"]
},
"include": ["src", "src/global.d.ts", "src/emotion.d.ts"]
}
还应该注意,尽管在输入 VS 代码时类型确实出现在自动完成中
解决方案
使用 create-react-app 时,我的情感主题类型可以进入:
emotion.d.ts
但是其他的全局类型,比如我扩展的window,需要往里面走:
index.d.ts
这些文件需要在src文件夹下,需要到tsconfig下面
"include": ["src/*"]
推荐阅读
- python - 具有单个输出的 2 个时间序列之间的互相关
- node.js - doc.data() 在创建 doc 然后在 firestore 中设置后不是函数错误
- selenium - 使用 JDK 15 和 Selenium 3.141.59 无法访问类型包错误
- swiftui - 找不到名为“airport.express”的系统映像
- ios - 如何在 JavaScript 中解密 Swift CryptoKit chacha20-poly1305 密码?
- python - 为什么 reticulate 在获取 Python 脚本时会产生 FileNotFound 错误?
- c++ - c++模板和继承,模板可以更有选择性吗
- linux - awk 重命名了一些文件,并留下了一些要重命名的文件
- python - 如何对dict中的值求和?蟒蛇,烧瓶
- ios - 如何使用 ios 谷歌日历中的链接添加事件