首页 > 解决方案 > 创建 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 代码时类型确实出现在自动完成中

标签: reactjstypescriptcreate-react-appemotion

解决方案


使用 create-react-app 时,我的情感主题类型可以进入:

emotion.d.ts

但是其他的全局类型,比如我扩展的window,需要往里面走:

index.d.ts

这些文件需要在src文件夹下,需要到tsconfig下面

"include": ["src/*"]

推荐阅读