【问题标题】:Create React App and Global Types not being picked up创建 React 应用程序和全局类型未被拾取
【发布时间】:2021-05-06 03:42:58
【问题描述】:

我有两个全局类型文件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 代码时类型确实出现在自动完成中

【问题讨论】:

  • 好的,我解决了情绪问题。我的主题文件中仍然有一些使用情感 9 样式的不正确的情感导入。但是,当我重新添加全局类型文件时,情绪会被拾取,但不再是全局类型
  • 嗨,AlexderKaran,你终于解决了这个问题吗?
  • 嗨@Tirias,我做到了。抱歉完全忘记了这一点。将添加一个答案

标签: reactjs typescript create-react-app emotion


【解决方案1】:

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

emotion.d.ts

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

index.d.ts

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

"include": ["src/*"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 2018-06-30
    • 2018-04-09
    • 1970-01-01
    • 2017-07-22
    • 2019-11-29
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多