Skip to content
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

Improve Types for Hooks #1460

Merged
merged 1 commit into from
Mar 11, 2021
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ flow-typed/
packages/**/dist/
examples/**/static/
*.ts
*.tsx
34 changes: 19 additions & 15 deletions packages/jss/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,36 @@ import {Properties as CSSProperties} from 'csstype'
// TODO: refactor to only include Observable types if plugin is installed.
import {Observable} from 'indefinite-observable'

// TODO: Type data better, currently typed as any for allowing to override it
type Func<R> = ((data: any) => R)
type Func<P, T, R> = T extends undefined ? ((data: P) => R) : ((data: P & {theme: T}) => R)
ITenthusiasm marked this conversation as resolved.
Show resolved Hide resolved

type NormalCssProperties = CSSProperties<string | number>
type NormalCssValues<K> = K extends keyof NormalCssProperties
? NormalCssProperties[K]
: JssValue
type NormalCssValues<K> = K extends keyof NormalCssProperties ? NormalCssProperties[K] : JssValue

export type JssStyle =
export type JssStyle<Props = any, Theme = undefined> =
| {
[K in keyof NormalCssProperties]:
| NormalCssValues<K>
| JssStyle
| Func<NormalCssValues<K> | JssStyle | undefined>
| JssStyle<Props, Theme>
| Func<Props, Theme, NormalCssValues<K> | JssStyle<undefined, undefined> | undefined>
| Observable<NormalCssValues<K> | JssStyle | undefined>
}
| {
[K: string]:
| JssValue
| JssStyle
| Func<JssValue | JssStyle | undefined>
| JssStyle<Props, Theme>
| Func<Props, Theme, JssValue | JssStyle<undefined, undefined> | undefined>
| Observable<JssValue | JssStyle | undefined>
}

export type Styles<Name extends string | number | symbol = string> = Record<
export type Styles<
Name extends string | number | symbol = string,
ITenthusiasm marked this conversation as resolved.
Show resolved Hide resolved
Props = unknown,
Theme = undefined
> = Record<
Name,
| JssStyle
| JssStyle<Props, Theme>
| string
| Func<JssStyle | string | null | undefined>
| Func<Props, Theme, JssStyle<undefined, undefined> | string | null | undefined>
| Observable<JssStyle | string | null | undefined>
>
export type Classes<Name extends string | number | symbol = string> = Record<Name, string>
Expand Down Expand Up @@ -215,7 +216,10 @@ export interface StyleSheet<RuleName extends string | number | symbol = string |
* Create and add rules.
* Will render also after Style Sheet was rendered the first time.
*/
addRules(styles: Partial<Styles<RuleName>>, options?: Partial<RuleOptions>): Rule[]
addRules(
styles: Partial<Styles<RuleName, any, undefined>>,
options?: Partial<RuleOptions>
): Rule[]
/**
* Get a rule by name.
*/
Expand Down Expand Up @@ -250,7 +254,7 @@ export interface JssOptions {

export interface Jss {
createStyleSheet<Name extends string | number | symbol>(
styles: Partial<Styles<Name>>,
styles: Partial<Styles<Name, any, undefined>>,
options?: StyleSheetFactoryOptions
): StyleSheet<Name>
removeStyleSheet(sheet: StyleSheet): this
Expand Down
42 changes: 37 additions & 5 deletions packages/jss/tests/types/Styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ interface Props {
flag?: boolean
}

interface Theme {
color: string
}

declare const color$: Observable<'cyan'>
declare const style$: Observable<{
backgroundColor: 'fuchsia'
transform: 'translate(0px, 205px)'
}>

const styles: Styles = {
// General Types Check
const styles: Styles<string, Props> = {
basic: {
textAlign: 'center',
display: 'flex',
Expand All @@ -22,7 +27,7 @@ const styles: Styles = {
textAlign: 'center',
display: 'flex',
width: '100%',
justifyContent: (props: Props) => (props.flag ? 'center' : undefined)
justifyContent: props => (props.flag ? 'center' : undefined)
},
inner: {
textAlign: 'center',
Expand All @@ -33,7 +38,7 @@ const styles: Styles = {
fontSize: 12
}
},
func: (props: Props) => ({
func: props => ({
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
Expand All @@ -43,8 +48,8 @@ const styles: Styles = {
position: 'relative',
pointerEvents: props.flag ? 'none' : null
}),
funcNull: (props: Props) => null,
funcWithTerm: (props: Props) => ({
funcNull: props => null,
funcWithTerm: props => ({
width: props.flag ? 377 : 272,
height: props.flag ? 330 : 400,
boxShadow: '0px 2px 20px rgba(0, 0, 0, 0.08)',
Expand All @@ -67,3 +72,30 @@ const styles: Styles = {
to: {opacity: 1}
}
}

// Test supplied Props and Theme
// Verify that nested parameter declarations are banned
const stylesPropsAndTheme: Styles<string, Props, Theme> = {
rootParamDeclaration: ({flag, theme}) => ({
fontWeight: 'bold',
// @ts-expect-error
nothingAllowed: ({flag, theme}) => ''
}),
anotherClass: {
color: 'red',
innerParamDeclaration1: ({flag, theme}) => '',
innerParamDeclaration2: ({flag, theme}) => ({
backgroundColor: 'blue',
// @ts-expect-error
nothingAllowed: ({flag, theme}) => ''
})
}
}

// Test the className types
const stylesClassNames: Styles<number, unknown, unknown> = {
// @ts-expect-error
stringClassName: '',
[1]: '',
[2]: ''
}
31 changes: 17 additions & 14 deletions packages/react-jss/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ declare const JssContext: Context<{
disableStylesGeneration: boolean
}>

type ClassesForStyles<S extends Styles | ((theme: any) => Styles)> = Classes<
S extends (theme: any) => Styles ? keyof ReturnType<S> : keyof S
>
type ClassesForStyles<
S extends Styles<any, any, any> | ((theme: any) => Styles<any, any, undefined>)
> = Classes<S extends (theme: any) => Styles<any, any, undefined> ? keyof ReturnType<S> : keyof S>

interface WithStylesProps<S extends Styles | ((theme: any) => Styles)> {
interface WithStylesProps<
S extends Styles<any, any, any> | ((theme: any) => Styles<any, any, undefined>)
> {
classes: ClassesForStyles<S>
}
/**
* @deprecated Please use `WithStylesProps` instead
*/
type WithStyles<S extends Styles | ((theme: any) => Styles)> = WithStylesProps<S>
type WithStyles<
S extends Styles<any, any, any> | ((theme: any) => Styles<any, any, undefined>)
> = WithStylesProps<S>

declare global {
namespace Jss {
Expand All @@ -72,26 +76,25 @@ interface CreateUseStylesOptions<Theme = DefaultTheme> extends BaseOptions<Theme
name?: string
}

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

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

declare function withStyles<
ClassNames extends string | number | symbol,
S extends Styles<ClassNames> | ((theme: any) => Styles<ClassNames>)
>(
styles: S,
declare function withStyles<ClassNames extends string | number | symbol, Props, Theme>(
styles:
| Styles<ClassNames, Props, Theme>
| ((theme: Theme) => Styles<ClassNames, Props, undefined>),
options?: WithStylesOptions
): <C>(
comp: C
) => ComponentType<
JSX.LibraryManagedAttributes<
C,
Omit<GetProps<C>, 'classes'> & {
classes?: Partial<ClassesForStyles<S>>
classes?: Partial<ClassesForStyles<typeof styles>>
innerRef?: RefObject<any> | ((instance: any) => void)
}
>
Expand Down
Loading