[英]How to specify global type declarations in create-react-app?
I am having trouble with setting global types in my create-react-app project.我在我的 create-react-app 项目中设置全局类型时遇到问题。 Here is my tsconfig.json file.
这是我的tsconfig.json文件。
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"components/*": [
"src/app/components/*"
],
"constants/*": [
"src/app/constants/*"
],
"services/*": [
"src/app/services/*"
],
"reducers/*": [
"src/app/reducers/*"
],
"selectors/*": [
"src/app/selectors/*"
],
"types/*": [
"src/app/types/*"
],
"pages/*": [
"src/app/pages/*"
],
"styles/*": [
"src/static/styles/*"
],
},
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"noImplicitAny": false,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"typeRoots": ["node_modules", "typings"],
},
"include": [
"src"
],
"exclude": ["node_modules", "**/*.js"]
}
As you can see, I have created a typings folder at the root of the project where I am going to keep global typings and some other typings.如您所见,我在项目的根目录下创建了一个类型文件夹,我将在其中保存全局类型和其他一些类型。 So in my typings folder, I have a global.d.ts file that has this declaration.
所以在我的打字文件夹中,我有一个包含此声明的global.d.ts文件。
declare global {
interface Window { fastlink: any; }
}
But inside my react component I have this typescript error但是在我的反应组件中,我有这个 typescript 错误
Property 'fastlink' does not exist on type 'Window & typeof globalThis'.
类型“Window & typeof globalThis”上不存在属性“fastlink”。
I have already investigated a lot of articles and posts here, also have read the docs, but with no result.我已经在这里调查了很多文章和帖子,也阅读了文档,但没有结果。
interface Window {
fastlink: any;
}
no need for declare global, you can directly define it in interface Window.不需要声明全局,可以直接在接口Window中定义。
you can access it by doing window.fastlink
您可以通过
window.fastlink
访问它
Be careful with using global variables.小心使用全局变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.