Skip to content

fix: export module augmentation for consuming applications #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"typecheck": "tsc --noEmit",
"build:esm": "tsc",
"build:cjs": "tsc --module commonjs --outDir dist/cjs",
"build": "rm -rf dist && rm -f .tsbuildinfo && npm run build:esm && npm run build:cjs",
"build:type-augmentation": "cp -r src/type-augmentation dist/type-augmentation",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exists because Typescript does not by default include any source .d.ts files in the output.

Potentially these could just be .ts files, in whcih case they would be included. But keeping them as .d.ts 100% ensures they have no runtime impact.

"build": "rm -rf dist && rm -f .tsbuildinfo && npm run build:esm && npm run build:cjs && npm run build:type-augmentation",
"lint-check": "eslint src/ .storybook/",
"lint-fix": "yarn lint-check --fix",
"fmt-check": "prettier --ignore-path .gitignore --ignore-path .prettierignore --check .",
Expand All @@ -37,6 +38,11 @@
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
}
},
"./type-augmentation": {
"import": "./dist/type-augmentation/index.d.ts",
"require": "./dist/type-augmentation/index.d.ts",
"default": "./dist/type-augmentation/index.d.ts"
}
},
"dependencies": {
Expand Down
7 changes: 1 addition & 6 deletions src/components/ThemeProvider/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as typography from "./typography"
import * as buttons from "./buttons"
import * as chips from "./chips"
import { colors } from "./colors"
import type { CustomTheme } from "../../types/theme"

const shadow = {
shadowOffsetX: 3,
Expand Down Expand Up @@ -94,10 +93,6 @@ const defaultThemeOptions: ThemeOptions = {
},
}

type ExtendedTheme = Theme & {
custom: CustomTheme
}

/**
* Create a customized Smoot Design theme for use with `ThemeProvider`.
*
Expand All @@ -106,7 +101,7 @@ type ExtendedTheme = Theme & {
*/
const createTheme = (options?: {
custom: Partial<ThemeOptions["custom"]>
}): ExtendedTheme =>
}): Theme =>
muiCreateTheme({
...defaultThemeOptions,
custom: {
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use client"
/// <reference types="./types/theme.d.ts" />
/// <reference types="./types/typography.d.ts" />

export { default as styled } from "@emotion/styled"
export { css, Global } from "@emotion/react"
Expand Down
17 changes: 17 additions & 0 deletions src/type-augmentation/TypescriptDocs.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Meta } from "@storybook/blocks"

<Meta title="Smoot-Design/Typescript" />

# Type Augmentation

Smoot Design uses Typescript's module augmentation to enhance types provided by `@emotion` and `@mui` packages. For example, we add custom theme properties (`theme.custom`) to the default theme and add new properties / values to some MUI components.

To enable this module augmentation in a consuming application or library, either:

- **Method 1:** `"@mitodl/smoot-design/type-augmentation"` to the `"types"` array in the `tsconfig.json` file, or
- **Method 2:** add `import "@mitodl/smoot-design/type-augmentation";` to a `global.d.ts` file.

For more information on this subject, see:

- TS Documenation: [Declaration Merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html)
- MUI Documentation: [Theming/Typescript](https://mui.com/material-ui/customization/theming/#typescript)
2 changes: 2 additions & 0 deletions src/type-augmentation/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "./theme"
import "./typography"
3 changes: 1 addition & 2 deletions src/types/theme.d.ts → src/type-augmentation/theme.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type { Theme as MuiTheme } from "@mui/material/styles"
import "@emotion/react"
import "@emotion/styled"
import "./typography"

export interface ColorGroup {
main: string
highlight: string
contrastText: string
}

interface CustomTheme {
export interface CustomTheme {
transitionDuration: string
shadow: string
colors: {
Expand Down
File renamed without changes.