Theme issue in a Typescript Monorepo #3938
-
|
I faced an issue with a typescript monorepo and theming using styled components, where if the project has a root tsconfig and we declare individual themes in each package, typescript expects all the themes to be exactly the same i.e. you cannot have multiple different themes in different packages. I have created a minimal reproduction using CRA and Yarn workspaces here. When you clone the repository and install all the dependencies locally and open the project in an editor, you would see that the theme from one app expects the keys from the theme of the other app. Is there any way to solve this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
Have you tried using a tsconfig file in each package? If you define the |
Beta Was this translation helpful? Give feedback.
-
|
In a TypeScript monorepo, Solutions:
const themed = <T extends object>(fn: (theme: T) => string) => fn;Option 1 (shared theme shape, different values) is the recommended approach. |
Beta Was this translation helpful? Give feedback.
In a TypeScript monorepo,
DefaultThememodule augmentation is global — all packages share the same augmented type. This is a TypeScript limitation, not a styled-components one.Solutions:
Shared theme type: Define one
DefaultThemeaugmentation in a shared package that all apps use. Each app can then provide different values conforming to the same shape.Per-package tsconfig: Ensure each package has its own
tsconfig.jsonwith a distinctrootDirand doesn't include declaration files from sibling packages.Generic wrapper: Instead of augmenting
DefaultTheme, use a generic wrapper that types the theme per-component:Op…