首页 > 解决方案 > 打字稿模块扩充是否会自动导出到组件库中?

问题描述

material-ui我在后台使用和 Typescript创建了一个组件库。我使用 Typescript 模块扩充来为主题添加新选项,如他们使用 Typescript文档进行主题定制中所述。

// createPalette.d.ts/* eslint-disable @typescript-eslint/no-unused-vars */
import { Palette, PaletteOptions } from "@material-ui/core/styles/createPalette";
declare module "@material-ui/core/styles/createPalette" {
    interface Palette {
        highlight: PaletteColor;
        layout: LayoutPaletteColor;
    }
    interface PaletteOptions {
        highlight: PaletteColorOptions;
        layout?: LayoutPaletteColor;
    }
    interface LayoutPaletteColorOptions {
        paper: string;
        text: string;
    }

    interface SidebarPaletteColorOptions extends LayoutPaletteColorOptions {
        navItemSelectedBg: string;
        navItemSelectedColor: string;
    }
    interface LayoutPaletteColor {
        header: LayoutPaletteColorOptions;
        sidebar: SidebarPaletteColorOptions;
        footer: LayoutPaletteColorOptions;
    }
}

然后,我构建我的项目并将其发布到 Github 包。我也在使用这个脚本,它将文件复制到*.d.ts文件dist

"copy-typings": "copyfiles -u 1 \"./src/types/*.d.ts\" dist",

例如,当我在另一个项目中安装包并引用 Header 时,我收到此错误

Cannot read property 'header' of undefined指向theme.palette.layout.header.paper引用的行。

错误

这让我相信 mycreatePalette.d.ts只对组件库本身有效,而不是当组件在另一个项目中使用时。

如何导出这个模块扩充?或者如何使这项工作?

标签: reactjstypescriptmaterial-uimodule-augmentation

解决方案


推荐阅读