Skip to content

Commit

Permalink
update typings
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantneal committed Aug 6, 2021
1 parent f810d85 commit f762463
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 88 deletions.
3 changes: 3 additions & 0 deletions packages/core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export type CSS<
Config['utils']
>

/** Returns the properties, attributes, and children expected by a component. */
export type ComponentProps<Component> = Parameters<Component>[0]

/** Returns a type that expects a value to be a kind of CSS property value. */
export type PropertyValue<K extends keyof CSSUtil.CSSProperties> = { readonly [CSSUtil.$$PropertyValue]: K }

Expand Down
7 changes: 2 additions & 5 deletions packages/core/types/stitches.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,8 @@ export default interface Stitches<
}
): StyledComponent.CssComponent<
StyledComponent.StyledComponentType<Composers>,
StyledComponent.StyledComponentProps<Composers>,
Media,
Theme,
ThemeMap,
Utils
StyledComponent.StyledComponentProps<Composers> & { css?: CSS },
Media
>
},
}
Expand Down
41 changes: 18 additions & 23 deletions packages/core/types/styled-component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,13 @@ import type * as Util from './util'

/** Returns a new CSS Component. */
export interface CssComponent<
TagName = 'span',
Type = 'span',
Props = {},
Media = Default.Media,
Theme = {},
ThemeMap = Default.ThemeMap,
Utils = {},
TransformedProps = TransformProps<Props, Media>,
CSS = CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
Media = Default.Media
> {
<
Props extends {
css?: Props['css'] extends CSSUtil.CSS
? Props['css']
: CSS
}
>(
(
props?:
& Partial<TransformedProps>
& {
css?: Props['css']
}
& Props
& {
[name in number | string]: any
}
Expand All @@ -37,15 +23,22 @@ export interface CssComponent<
className: string
selector: string

[$$StyledComponentType]: TagName
[$$StyledComponentProps]: Props
[$$StyledComponentType]: Type
[$$StyledComponentProps]: Omit<Props, 'css'>
[$$StyledComponentMedia]: Media
}

export type TransformProps<Props, Media> = {
[K in keyof Props]: Props[K] | {
[KMedia in Util.Prefixed<'@', 'initial' | keyof Media>]?: Props[K]
}
[K in keyof Props]:
K extends 'css'
? Props['css'] extends CSSUtil.CSS
? Props['css']
: CSSUtil.CSS
:
| Props[K]
| {
[KMedia in Util.Prefixed<'@', 'initial' | keyof Media>]?: Props[K]
}
}

/** Unique symbol used to reference the type of a Styled Component. */
Expand All @@ -72,6 +65,8 @@ export type StyledComponentType<T extends any[]> = (
? 'span'
: T[0] extends string
? T[0]
: T[0] extends (props: any) => any
? T[0]
: T[0] extends { [$$StyledComponentType]: unknown }
? T[0][$$StyledComponentType]
: T extends [lead: any, ...tail: infer V]
Expand Down
3 changes: 3 additions & 0 deletions packages/react/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export type CSS<
Config['utils']
>

/** Returns the properties, attributes, and children expected by a component. */
export type ComponentProps<Component> = Parameters<Component>[0]

/** Returns a type that expects a value to be a kind of CSS property value. */
export type PropertyValue<K extends keyof CSSUtil.CSSProperties> = { readonly [CSSUtil.$$PropertyValue]: K }

Expand Down
18 changes: 6 additions & 12 deletions packages/react/types/stitches.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,13 @@ export default interface Stitches<
}
): StyledComponent.CssComponent<
StyledComponent.StyledComponentType<Composers>,
StyledComponent.StyledComponentProps<Composers>,
Media,
Theme,
ThemeMap,
Utils
StyledComponent.StyledComponentProps<Composers> & { css?: CSS },
Media
>
},
styled: {
<
DefaultType extends (
Type extends (
| string
| React.ExoticComponent<any>
| React.JSXElementConstructor<any>
Expand All @@ -159,7 +156,7 @@ export default interface Stitches<
| { [name: string]: unknown }
)[]
>(
type: DefaultType,
type: Type,
...composers: {
[K in keyof Composers]: (
Composers[K] extends string
Expand Down Expand Up @@ -215,11 +212,8 @@ export default interface Stitches<
}
): StyledComponent.StyledComponent<
DefaultType,
StyledComponent.StyledComponentProps<Composers>,
Media,
Theme,
ThemeMap,
Utils
StyledComponent.StyledComponentProps<Composers> & { css?: CSS },
Media
>
}
}
Expand Down
89 changes: 41 additions & 48 deletions packages/react/types/styled-component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,39 @@ import type * as Util from './util'
export interface StyledComponent<
Type = 'span',
Props = {},
Media = Default.Media,
Theme = {},
ThemeMap = Default.ThemeMap,
Utils = {},
TransformedProps = TransformProps<Props, Media>,
CSS = CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
> extends ForwardRefExoticComponent<Type, TransformedProps> {
<
As = Type,
Props extends {
css?: Props['css'] extends CSSUtil.CSS
? Props['css']
: CSS
}
>(
props: (
As extends keyof JSX.IntrinsicElements
? Util.Assign<JSX.IntrinsicElements[As], Partial<TransformedProps & { as: As, css: Props['css'] }>>
: As extends React.ComponentType<infer P>
? Util.Assign<P, Partial<TransformedProps & { as: As, css: Props['css'] }>>
: never
)
Media = Default.Media
> extends React.ForwardRefExoticComponent<
Util.Assign<
Type extends React.ElementType
? React.ComponentPropsWithRef<Type>
: never,
Props & { as?: Type }
>
> {
<As = Type>(
props: As extends ''
? { as: keyof JSX.IntrinsicElements }
: As extends React.ComponentType<infer P>
? Util.Assign<P, Props & { as: As }>
: As extends keyof JSX.IntrinsicElements
? Util.Assign<JSX.IntrinsicElements[As], Props & { as: As }>
: never
): React.ReactElement | null

[$$StyledComponentType]: Type
[$$StyledComponentProps]: Props
[$$StyledComponentProps]: Omit<Props, 'css'>
[$$StyledComponentMedia]: Media
}

/** Returns a new CSS Component. */
export interface CssComponent<
TagName = 'span',
Type = 'span',
Props = {},
Media = Default.Media,
Theme = {},
ThemeMap = Default.ThemeMap,
Utils = {},
TransformedProps = TransformProps<Props, Media>,
CSS = CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
Media = Default.Media
> {
<
Props extends {
css?: Props['css'] extends CSSUtil.CSS
? Props['css']
: CSS
}
>(
(
props?:
& Partial<TransformedProps>
& {
css?: Props['css']
}
& Props
& {
[name in number | string]: any
}
Expand All @@ -71,15 +52,22 @@ export interface CssComponent<
className: string
selector: string

[$$StyledComponentType]: TagName
[$$StyledComponentProps]: Props
[$$StyledComponentType]: Type
[$$StyledComponentProps]: Omit<Props, 'css'>
[$$StyledComponentMedia]: Media
}

export type TransformProps<Props, Media> = {
[K in keyof Props]: Props[K] | {
[KMedia in Util.Prefixed<'@', 'initial' | keyof Media>]?: Props[K]
}
[K in keyof Props]:
K extends 'css'
? Props['css'] extends CSSUtil.CSS
? Props['css']
: CSSUtil.CSS
:
| Props[K]
| {
[KMedia in Util.Prefixed<'@', 'initial' | keyof Media>]?: Props[K]
}
}

/** Unique symbol used to reference the type of a Styled Component. */
Expand All @@ -104,8 +92,13 @@ export type $$StyledComponentMedia = typeof $$StyledComponentMedia
type IntrinsicElement<TagName> = TagName extends keyof JSX.IntrinsicElements ? TagName : never

/** Returns a ForwardRef component. */
type ForwardRefExoticComponent<ElementType, Props> = React.ForwardRefExoticComponent<
Util.Assign<ElementType extends React.ElementType ? React.ComponentPropsWithRef<ElementType> : never, Props>
type ForwardRefExoticComponent<Type, Props> = React.ForwardRefExoticComponent<
Util.Assign<
Type extends React.ElementType
? React.ComponentPropsWithRef<Type>
: never,
Props & { as?: Type }
>
>

/** Returns the first Styled Component type from the given array of compositions. */
Expand Down

0 comments on commit f762463

Please sign in to comment.