繁体   English   中英

如何在 create-react-app 中指定全局类型声明?

[英]How to specify global type declarations in create-react-app?

我在我的 create-react-app 项目中设置全局类型时遇到问题。 这是我的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"]
}

如您所见,我在项目的根目录下创建了一个类型文件夹,我将在其中保存全局类型和其他一些类型。 所以在我的打字文件夹中,我有一个包含此声明的global.d.ts文件。

declare global {
    interface Window { fastlink: any; }
}

但是在我的反应组件中,我有这个 typescript 错误

类型“Window & typeof globalThis”上不存在属性“fastlink”。

我已经在这里调查了很多文章和帖子,也阅读了文档,但没有结果。

interface Window { 
  fastlink: any;
}

不需要声明全局,可以直接在接口Window中定义。

您可以通过window.fastlink访问它

小心使用全局变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM