Skip to content

Commit 56205c1

Browse files
authored
support global typescript theme definition (#1453)
1 parent debfcfa commit 56205c1

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

contributing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ yarn build
2626
To lint, format, and type check the project run the following commands:
2727

2828
```bash
29-
yarn lint
29+
yarn check:lint
3030
yarn format
31-
yarn typecheck
31+
yarn check:ts
3232
```
3333

3434
## Run tests

packages/react-jss/src/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ interface WithStylesProps<S extends Styles | ((theme: any) => Styles)> {
4545
*/
4646
type WithStyles<S extends Styles | ((theme: any) => Styles)> = WithStylesProps<S>
4747

48-
export interface DefaultTheme {}
48+
declare global {
49+
namespace Jss {
50+
/** You can use the global `Jss.Theme` interface to define a project-wide default theme. */
51+
export interface Theme {}
52+
}
53+
}
54+
55+
export type DefaultTheme = Jss.Theme
4956

5057
interface BaseOptions<Theme = DefaultTheme> extends StyleSheetFactoryOptions {
5158
index?: number

packages/react-jss/src/index.test.tsx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react'
2+
import WithStyles, {createUseStyles} from '.'
23

3-
import WithStyles from '.'
4+
const expectType = <T extends any>(x: T): T => x
45

56
interface Props {
67
testProp: string
@@ -43,3 +44,39 @@ function testRender() {
4344
</>
4445
)
4546
}
47+
48+
declare global {
49+
namespace Jss {
50+
export interface Theme {
51+
themeColour: string
52+
defaultFontSize: number
53+
}
54+
}
55+
}
56+
57+
const useStyles = createUseStyles(theme => {
58+
expectType<string>(theme.themeColour)
59+
expectType<number>(theme.defaultFontSize)
60+
61+
return {
62+
myDiv: {
63+
color: theme.themeColour,
64+
// @ts-expect-error typescript should error here since the theme property doesn't exist
65+
border: theme.aPropertyThatDoesntExist
66+
}
67+
}
68+
})
69+
70+
export function Component() {
71+
const classes = useStyles()
72+
73+
expectType<string>(classes.myDiv)
74+
75+
return (
76+
<>
77+
<div className={classes.myDiv} />
78+
{/* @ts-expect-error typescript should error here since the class is invalid */}
79+
<div className={classes.thisClassDoesntExist} />
80+
</>
81+
)
82+
}

0 commit comments

Comments
 (0)