Skip to content

Commit 9d0eb91

Browse files
committed
Reorder Styles (and Related) Generic Parameters
* Styles (and related) generic type parameters are now ordered as `<Name, Props, Theme>`. * Updated tests. * Added `withStyles` test for plain objects without typedefs.
1 parent bc118e9 commit 9d0eb91

5 files changed

Lines changed: 56 additions & 35 deletions

File tree

packages/jss/src/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ export type JssStyle<Props = any, Theme = undefined> =
3131
}
3232

3333
export type Styles<
34+
Name extends string | number | symbol = string,
3435
Props = unknown,
35-
Theme = undefined,
36-
Name extends string | number | symbol = string
36+
Theme = undefined
3737
> = Record<
3838
Name,
3939
| JssStyle<Props, Theme>
@@ -219,7 +219,7 @@ export interface StyleSheet<RuleName extends string | number | symbol = string |
219219
* Will render also after Style Sheet was rendered the first time.
220220
*/
221221
addRules(
222-
styles: Partial<Styles<any, undefined, RuleName>>,
222+
styles: Partial<Styles<RuleName, any, undefined>>,
223223
options?: Partial<RuleOptions>
224224
): Rule[]
225225
/**
@@ -256,7 +256,7 @@ export interface JssOptions {
256256

257257
export interface Jss {
258258
createStyleSheet<Name extends string | number | symbol>(
259-
styles: Partial<Styles<any, undefined, Name>>,
259+
styles: Partial<Styles<Name, any, undefined>>,
260260
options?: StyleSheetFactoryOptions
261261
): StyleSheet<Name>
262262
removeStyleSheet(sheet: StyleSheet): this

packages/jss/tests/types/Styles.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ declare const style$: Observable<{
1616
}>
1717

1818
// General Types Check
19-
const styles: Styles<Props> = {
19+
const styles: Styles<string, Props> = {
2020
basic: {
2121
textAlign: 'center',
2222
display: 'flex',
@@ -75,7 +75,7 @@ const styles: Styles<Props> = {
7575

7676
// Test supplied Props and Theme
7777
// Verify that nested parameter declarations are banned
78-
const stylesPropsAndTheme: Styles<Props, Theme> = {
78+
const stylesPropsAndTheme: Styles<string, Props, Theme> = {
7979
rootParamDeclaration: ({flag, theme}) => ({
8080
fontWeight: 'bold',
8181
// @ts-expect-error
@@ -93,7 +93,7 @@ const stylesPropsAndTheme: Styles<Props, Theme> = {
9393
}
9494

9595
// Test the className types
96-
const stylesClassNames: Styles<unknown, unknown, number> = {
96+
const stylesClassNames: Styles<number, unknown, unknown> = {
9797
// @ts-expect-error
9898
stringClassName: '',
9999
[1]: '',

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ declare const JssContext: Context<{
3838
}>
3939

4040
type ClassesForStyles<
41-
S extends Styles<any, any, any> | ((theme: any) => Styles<any, undefined, any>)
42-
> = Classes<S extends (theme: any) => Styles<any, undefined, any> ? keyof ReturnType<S> : keyof S>
41+
S extends Styles<any, any, any> | ((theme: any) => Styles<any, any, undefined>)
42+
> = Classes<S extends (theme: any) => Styles<any, any, undefined> ? keyof ReturnType<S> : keyof S>
4343

4444
interface WithStylesProps<
45-
S extends Styles<any, any, any> | ((theme: any) => Styles<any, undefined, any>)
45+
S extends Styles<any, any, any> | ((theme: any) => Styles<any, any, undefined>)
4646
> {
4747
classes: ClassesForStyles<S>
4848
}
4949
/**
5050
* @deprecated Please use `WithStylesProps` instead
5151
*/
5252
type WithStyles<
53-
S extends Styles<any, any, any> | ((theme: any) => Styles<any, undefined, any>)
53+
S extends Styles<any, any, any> | ((theme: any) => Styles<any, any, undefined>)
5454
> = WithStylesProps<S>
5555

5656
declare global {
@@ -76,17 +76,17 @@ interface CreateUseStylesOptions<Theme = DefaultTheme> extends BaseOptions<Theme
7676
name?: string
7777
}
7878

79-
declare function createUseStyles<Props = unknown, Theme = DefaultTheme, C extends string = string>(
80-
styles: Styles<Props, Theme, C> | ((theme: Theme) => Styles<Props, undefined, C>),
79+
declare function createUseStyles<C extends string = string, Props = unknown, Theme = DefaultTheme>(
80+
styles: Styles<C, Props, Theme> | ((theme: Theme) => Styles<C, Props, undefined>),
8181
options?: CreateUseStylesOptions<Theme>
8282
): (data?: Props & {theme?: Theme}) => Classes<C>
8383

8484
type GetProps<C> = C extends ComponentType<infer P> ? P : never
8585

86-
declare function withStyles<Props, Theme, ClassNames extends string | number | symbol>(
86+
declare function withStyles<ClassNames extends string | number | symbol, Props, Theme>(
8787
styles:
88-
| Styles<Props, Theme, ClassNames>
89-
| ((theme: Theme) => Styles<Props, undefined, ClassNames>),
88+
| Styles<ClassNames, Props, Theme>
89+
| ((theme: Theme) => Styles<ClassNames, Props, undefined>),
9090
options?: WithStylesOptions
9191
): <C>(
9292
comp: C

packages/react-jss/tests/types/createUseStyles.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const themeArg2ClassesFail2 = themeArg2({theme: expectedCustomTheme})
3939
const themeArg2ClassesPass = themeArg2({theme: expectedDefaultTheme})
4040

4141
// Props declaration is allowed
42-
const themeArg3 = createUseStyles<MyProps>(theme => ({
42+
const themeArg3 = createUseStyles<string, MyProps>(theme => ({
4343
onlyPropsAllowed: ({...props}) => ({
4444
fontWeight: 'bold'
4545
})
@@ -52,7 +52,7 @@ const themeArg3ClassesPass = themeArg3(expectedCustomProps)
5252
const themeArg3ClassesPass2 = themeArg3({...expectedCustomProps, theme: expectedDefaultTheme})
5353

5454
// Nested props declaration banned
55-
const themeArg4 = createUseStyles<MyProps>(theme => ({
55+
const themeArg4 = createUseStyles<string, MyProps>(theme => ({
5656
onlyPropsAllowed: ({...props}) => ({
5757
fontWeight: 'bold',
5858
// @ts-expect-error
@@ -61,7 +61,7 @@ const themeArg4 = createUseStyles<MyProps>(theme => ({
6161
}))
6262

6363
// Supplied theme type is acknowledged
64-
const themeArg5 = createUseStyles<unknown, MyTheme>(theme => ({}))
64+
const themeArg5 = createUseStyles<string, unknown, MyTheme>(theme => ({}))
6565
// @ts-expect-error
6666
const themeArg5ClassesFail = themeArg5({theme: {}})
6767
// @ts-expect-error
@@ -129,7 +129,7 @@ const noThemeArg2ClassesFail2 = noThemeArg2({theme: expectedCustomTheme})
129129
const noThemeArg2ClassesPass = noThemeArg2({theme: expectedDefaultTheme})
130130

131131
// Props declaration is allowed, but not nested props declaration
132-
const noThemeArg3 = createUseStyles<MyProps>({
132+
const noThemeArg3 = createUseStyles<string, MyProps>({
133133
propsAndTheme: ({property, theme}) => ({
134134
fontWeight: 'bold',
135135
// @ts-expect-error
@@ -149,7 +149,7 @@ const noThemeArg3ClassesPass = noThemeArg3(expectedCustomProps)
149149
const noThemeArg3ClassesPass2 = noThemeArg3({...expectedCustomProps, theme: expectedDefaultTheme})
150150

151151
// Props and Theme types are properly acknowledged when supplied
152-
const noThemeArg4 = createUseStyles<MyProps, MyTheme>({
152+
const noThemeArg4 = createUseStyles<string, MyProps, MyTheme>({
153153
propsAndTheme: ({property, theme}) => ({
154154
fontWeight: 'bold',
155155
// @ts-expect-error
@@ -169,7 +169,7 @@ const noThemeArg4ClassesPass = noThemeArg4(expectedCustomProps)
169169
const noThemeArg4ClassesPass2 = noThemeArg4({...expectedCustomProps, theme: expectedCustomTheme})
170170

171171
// Nested declarations are banned (single nest test)
172-
const noThemeArg5 = createUseStyles<MyProps, MyTheme>({
172+
const noThemeArg5 = createUseStyles<string, MyProps, MyTheme>({
173173
singleNest: {
174174
fontWeight: 'bold',
175175
singleValue: ({property, theme}) => '',
@@ -182,7 +182,7 @@ const noThemeArg5 = createUseStyles<MyProps, MyTheme>({
182182
})
183183

184184
// Nested declarations are banned (double nest test)
185-
const noThemeArg6 = createUseStyles<MyProps, MyTheme>({
185+
const noThemeArg6 = createUseStyles<string, MyProps, MyTheme>({
186186
doubleNest: {
187187
fontWeight: 'bold',
188188
singleValue: ({property, theme}) => '',
@@ -199,7 +199,7 @@ const noThemeArg6 = createUseStyles<MyProps, MyTheme>({
199199
})
200200

201201
// Nested declarations are banned (triple nest test)
202-
const noThemeArg7 = createUseStyles<MyProps, MyTheme>({
202+
const noThemeArg7 = createUseStyles<string, MyProps, MyTheme>({
203203
tripleNest: {
204204
fontWeight: 'bold',
205205
singleValue: ({property, theme}) => '',

packages/react-jss/tests/types/withStyles.tsx

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,32 @@ let ResultingComponent: React.ComponentType<MyProps>
2424
let ComponentTest: React.FC
2525

2626
/* -------------------- Function Argument Passing Cases -------------------- */
27+
// Plain Object (no type supplied)
28+
function functionPlainObject(theme: MyTheme) {
29+
return {
30+
someClassName: '',
31+
anotherClassName: {
32+
fontWeight: 'bold'
33+
}
34+
}
35+
}
36+
ResultingComponent = withStyles(functionPlainObject)(SimpleComponent)
37+
ComponentTest = () => <ResultingComponent property="" />
2738

2839
// Plain Styles
29-
function functionPlain(theme: MyTheme): Styles {
40+
function functionPlainStyles(theme: MyTheme): Styles {
3041
return {
3142
someClassName: '',
3243
anotherClassName: {
3344
fontWeight: 'bold'
3445
}
3546
}
3647
}
37-
ResultingComponent = withStyles(functionPlain)(SimpleComponent)
48+
ResultingComponent = withStyles(functionPlainStyles)(SimpleComponent)
3849
ComponentTest = () => <ResultingComponent property="" />
3950

4051
// With Props
41-
function functionProps(theme: MyTheme): Styles<MyProps> {
52+
function functionProps(theme: MyTheme): Styles<string, MyProps> {
4253
return {
4354
someClassName: ({property}) => '',
4455
anotherClassName: {
@@ -50,7 +61,7 @@ ResultingComponent = withStyles(functionProps)(SimpleComponent)
5061
ComponentTest = () => <ResultingComponent property="" />
5162

5263
// With Props and ClassName rules
53-
function functionPropsAndName(theme: MyTheme): Styles<MyProps, undefined, number> {
64+
function functionPropsAndName(theme: MyTheme): Styles<number, MyProps, undefined> {
5465
return {
5566
[1]: ({property}) => '',
5667
[2]: {
@@ -63,6 +74,16 @@ ComponentTest = () => <ResultingComponent property="" />
6374

6475
/* -------------------- Regular Object Passing Cases -------------------- */
6576

77+
// Plain Object (no type supplied)
78+
const plainObject = {
79+
someClassName: '',
80+
anotherClassName: {
81+
fontWeight: 'bold'
82+
}
83+
}
84+
ResultingComponent = withStyles(plainObject)(SimpleComponent)
85+
ComponentTest = () => <ResultingComponent property="" />
86+
6687
// Plain Styles
6788
const stylesPlain: Styles = {
6889
someClassName: '',
@@ -74,7 +95,7 @@ ResultingComponent = withStyles(stylesPlain)(SimpleComponent)
7495
ComponentTest = () => <ResultingComponent property="" />
7596

7697
// With Props
77-
const stylesProps: Styles<MyProps> = {
98+
const stylesProps: Styles<string, MyProps> = {
7899
someClassName: ({property}) => '',
79100
anotherClassName: {
80101
fontWeight: 'bold'
@@ -84,7 +105,7 @@ ResultingComponent = withStyles(stylesProps)(SimpleComponent)
84105
ComponentTest = () => <ResultingComponent property="" />
85106

86107
// With Theme
87-
const stylesTheme: Styles<unknown, MyTheme> = {
108+
const stylesTheme: Styles<string, unknown, MyTheme> = {
88109
someClassName: ({theme}) => '',
89110
anotherClassName: {
90111
fontWeight: 'bold'
@@ -94,7 +115,7 @@ ResultingComponent = withStyles(stylesTheme)(SimpleComponent)
94115
ComponentTest = () => <ResultingComponent property="" />
95116

96117
// With Props and Theme
97-
const stylesPropsAndTheme: Styles<MyProps, MyTheme> = {
118+
const stylesPropsAndTheme: Styles<string, MyProps, MyTheme> = {
98119
someClassName: ({property, theme}) => '',
99120
anotherClassName: {
100121
fontWeight: 'bold'
@@ -104,7 +125,7 @@ ResultingComponent = withStyles(stylesPropsAndTheme)(SimpleComponent)
104125
ComponentTest = () => <ResultingComponent property="" />
105126

106127
// With Props and Theme and ClassName rules
107-
const stylesPropsAndThemeAndName: Styles<MyProps, MyTheme, number> = {
128+
const stylesPropsAndThemeAndName: Styles<number, MyProps, MyTheme> = {
108129
[1]: ({property, theme}) => '',
109130
[2]: {
110131
fontWeight: 'bold'
@@ -116,7 +137,7 @@ ComponentTest = () => <ResultingComponent property="" />
116137
/* -------------------- Failing Cases -------------------- */
117138

118139
// A function argument cannot provide another defined theme type conflicting with `undefined`
119-
function failingFunctionRedefineTheme(theme: MyTheme): Styles<unknown, any> {
140+
function failingFunctionRedefineTheme(theme: MyTheme): Styles<string, unknown, any> {
120141
return {
121142
someClassName: '',
122143
anotherClassName: {
@@ -125,7 +146,7 @@ function failingFunctionRedefineTheme(theme: MyTheme): Styles<unknown, any> {
125146
}
126147
}
127148

128-
function passingFunctionUnknownTheme(theme: MyTheme): Styles<unknown, unknown> {
149+
function passingFunctionUnknownTheme(theme: MyTheme): Styles<string, unknown, unknown> {
129150
return {
130151
someClassName: '',
131152
anotherClassName: {
@@ -134,7 +155,7 @@ function passingFunctionUnknownTheme(theme: MyTheme): Styles<unknown, unknown> {
134155
}
135156
}
136157

137-
function passingFunctionNullTheme(theme: MyTheme): Styles<unknown, null> {
158+
function passingFunctionNullTheme(theme: MyTheme): Styles<string, unknown, null> {
138159
return {
139160
someClassName: '',
140161
anotherClassName: {

0 commit comments

Comments
 (0)