8

I created a component library using material-ui and Typescript under the hood. I have used Typescript module augmentation in order to add new options to the theme, as described in their theme customization with Typescript documentation.

// 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;
    }
}

Then, I build my project and publish it to Github packages. I'm also using this script which copies the *.d.ts files to the dist folder

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

When I install the package in another project and reference the Header, for example, I get this error

Cannot read property 'header' of undefined which points at the line where the theme.palette.layout.header.paper is referenced.

error

This makes me believe that my createPalette.d.ts is only being effective on the component library itself, but not when the components are being used in another project.

How to export this module augmentation? Or how to make this work?

Bassem
  • 3,582
  • 2
  • 24
  • 47
  • 1
    Add an import to the file containing the augmentation to the entry points of your library. A bare import `import "./augmentations";` – Aluan Haddad Jan 18 '21 at 23:28
  • That didn't help – Bassem Jan 18 '21 at 23:34
  • Wait... I just realized you're getting a runtime error. That has nothing to do with augmentations in a `.d.ts` file which contains only declarations. You have a different problem. – Aluan Haddad Jan 18 '21 at 23:37
  • Please [edit] your post to add code and data as text ([using code formatting](https://stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](https://meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. See [mcve] on what code is required. – Adriaan Dec 08 '22 at 07:56
  • 1
    Has anyone been able to find a solution to this? – johnny peter Dec 31 '22 at 13:20
  • Just use something else other than Material UI... https://medium.com/javascript-in-plain-english/materialui-is-a-tech-debt-for-scalable-front-end-applications-4412c81ec9ee – Bassem May 29 '23 at 09:49

0 Answers0