-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[styles] Overload function signature instead of conditional (#19320)
- Loading branch information
Showing
7 changed files
with
70 additions
and
164 deletions.
There are no files selected for viewing
36 changes: 8 additions & 28 deletions
36
packages/material-ui-styles/src/makeStyles/makeStyles.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,27 @@ | ||
import { | ||
ClassKeyOfStyles, | ||
ClassNameMap, | ||
PropsOfStyles, | ||
Styles, | ||
WithStylesOptions, | ||
} from '@material-ui/styles/withStyles'; | ||
import { Omit, IsAny, Or, IsEmptyInterface } from '@material-ui/types'; | ||
import { Omit } from '@material-ui/types'; | ||
import { DefaultTheme } from '../defaultTheme'; | ||
|
||
/** | ||
* @internal | ||
* | ||
* If a style callback is given with `theme => stylesOfTheme` then typescript | ||
* infers `Props` to `any`. | ||
* If a static object is given with { ...members } then typescript infers `Props` | ||
* to `{}`. | ||
* | ||
* So we require no props in `useStyles` if `Props` in `makeStyles(styles)` is | ||
* inferred to either `any` or `{}` | ||
* `makeStyles` where the passed `styles` do not depend on props | ||
*/ | ||
export type StylesRequireProps<S> = Or< | ||
IsAny<PropsOfStyles<S>>, | ||
IsEmptyInterface<PropsOfStyles<S>> | ||
> extends true | ||
? false | ||
: true; | ||
|
||
export default function makeStyles<Theme = DefaultTheme, ClassKey extends string = string>( | ||
style: Styles<Theme, {}, ClassKey>, | ||
options?: Omit<WithStylesOptions<Theme>, 'withTheme'>, | ||
): (props?: any) => ClassNameMap<ClassKey>; | ||
/** | ||
* @internal | ||
* | ||
* `Props` are `any` either by explicit annotation or if there are no callbacks | ||
* from which the typechecker could infer a type so it falls back to `any`. | ||
* See the test cases for examples and implications of explicit `any` annotation | ||
* `makeStyles` where the passed `styles` do depend on props | ||
*/ | ||
export type StylesHook<S extends Styles<any, any>> = StylesRequireProps<S> extends false | ||
? (props?: any) => ClassNameMap<ClassKeyOfStyles<S>> | ||
: (props: PropsOfStyles<S>) => ClassNameMap<ClassKeyOfStyles<S>>; | ||
|
||
export default function makeStyles< | ||
Theme = DefaultTheme, | ||
Props extends {} = {}, | ||
ClassKey extends string = string | ||
>( | ||
styles: Styles<Theme, Props, ClassKey>, | ||
options?: Omit<WithStylesOptions<Theme>, 'withTheme'>, | ||
): StylesHook<Styles<Theme, Props, ClassKey>>; | ||
): (props: Props) => ClassNameMap<ClassKey>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
import { Theme as DefaultTheme } from './createMuiTheme'; | ||
import { Styles, WithStylesOptions } from '@material-ui/styles/withStyles'; | ||
import { StylesHook } from '@material-ui/styles/makeStyles'; | ||
import { ClassNameMap, Styles, WithStylesOptions } from '@material-ui/styles/withStyles'; | ||
|
||
import { Omit } from '@material-ui/types'; | ||
|
||
/** | ||
* `makeStyles` where the passed `styles` do not depend on props | ||
*/ | ||
export default function makeStyles<Theme = DefaultTheme, ClassKey extends string = string>( | ||
style: Styles<Theme, {}, ClassKey>, | ||
options?: Omit<WithStylesOptions<Theme>, 'withTheme'>, | ||
): (props?: any) => ClassNameMap<ClassKey>; | ||
/** | ||
* `makeStyles` where the passed `styles` do depend on props | ||
*/ | ||
export default function makeStyles< | ||
Theme = DefaultTheme, | ||
Props extends {} = {}, | ||
ClassKey extends string = string | ||
>( | ||
styles: Styles<Theme, Props, ClassKey>, | ||
options?: Omit<WithStylesOptions<Theme>, 'withTheme'>, | ||
): StylesHook<Styles<Theme, Props, ClassKey>>; | ||
|
||
export { StylesHook }; | ||
): (props: Props) => ClassNameMap<ClassKey>; |