Skip to content

Commit

Permalink
feat(typing): infer automatically the typing of the generators used i…
Browse files Browse the repository at this point in the history
…nside of the compose (#358)

Co-authored-by: Kamal Bennani <kamal.bennani@aircall.io>
  • Loading branch information
kamalbennani and kamalbennani-aircall committed Apr 2, 2022
1 parent c0c92b4 commit ecbae73
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 8 additions & 2 deletions packages/system/src/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
Mixin,
StyleOptions,
CSSOption,
StyleGeneratorPropsConcat,
} from './types'

let themeGetterId = 0
Expand Down Expand Up @@ -224,9 +225,14 @@ const sortStyles = (
return styles
}

export const compose = <TProps = {}>(
export function compose<TProps = {}>(
...generators: StyleGenerator[]
): StyleGenerator<TProps> => {
): StyleGenerator<TProps>
export function compose<T extends StyleGenerator[]>(
...generators: T
): StyleGenerator<StyleGeneratorPropsConcat<T>>

export function compose(...generators: any[]): any {
let flatGenerators: StyleGenerator[] = []

generators.forEach((gen) => {
Expand Down
10 changes: 6 additions & 4 deletions packages/system/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ export type ThemeGetterType<T extends ThemeGetter> = T extends ThemeGetter<
? T
: never

export type StyleGeneratorProps<T extends object> = T extends StyleGenerator<
infer T
>
? T
export type StyleGeneratorProps<T> = T extends StyleGenerator<infer Props>
? Props
: never

export type StyleGeneratorPropsConcat<T> = T extends [infer Head, ...infer Tail]
? StyleGeneratorProps<Head> & StyleGeneratorPropsConcat<Tail>
: unknown

0 comments on commit ecbae73

Please sign in to comment.