为什么我的 .d.ts 接口扩展不起作用?

Ian*_*Ian 6 typescript material-ui typescript-typings

我正在经历一场真正的战斗,试图让.d.ts文件在单一存储库中正常工作。我想要做的是扩展一个material-ui接口,然后导出更新的类型以在多个其他地方使用。

\n

类型/src/material-ui.d.ts

\n
declare module "@material-ui/core" {\n    interface CommonColors {\n        amberGreen: string;\n        green2: string;\n        red2: string;\n        yellow2: string;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

类型/src/index.ts

\n
import "./material-ui.d.ts";\n\nexport {};\n
Run Code Online (Sandbox Code Playgroud)\n

我已经尝试了各种导入方法,并搜索了许多 StackOverflow 问题,但我无法使其工作。red2在我的另一个包中总是未定义。目前我正在使用:

\n

mui-测试/src/index.ts

\n
import { useTheme } from "@material-ui/core";\nimport "@iw/types";\n\nexport const useMUITest = (): void => {\n    const theme = useTheme();\n\n    console.log(theme.palette);\n    console.log(theme.palette.common);\n    console.log(theme.palette.common.red2);\n};\n
Run Code Online (Sandbox Code Playgroud)\n

我在这里完整重现了该问题https://github.com/IPWright83/pnpm-monorepo/tree/d-ts

\n
@iw/mui-test:build: src/index.ts(9,38): error TS2339: Property \'red2\' does not exist on type \'CommonColors\'.\n@iw/mui-test:build: \xe2\x80\x89ELIFECYCLE\xe2\x80\x89 Command failed with exit code 2.\n@iw/mui-test:build: ERROR: command finished with error: command (packages/mui-test) pnpm run build exited (1)\n
Run Code Online (Sandbox Code Playgroud)\n

mat*_*rai 0

您的文件material-ui.d.ts首先应该被命名index.d.ts。您export还应该使用关键字,例如:

declare module "@material-ui/core" {
    export interface CommonColors {
        amberGreen: string;
        green2: string;
        red2: string;
        yellow2: string;
    }
}
Run Code Online (Sandbox Code Playgroud)

默认情况下,您还应该在其定义文件中导出接口,例如:

export interface CommonColors {
    amberGreen: string;
    green2: string;
    red2: string;
    yellow2: string;
}
export default CommonColors;
Run Code Online (Sandbox Code Playgroud)