Skip to content

Commit

Permalink
fix more ts issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hadihallak committed Aug 26, 2021
1 parent 0c23a52 commit 7b35658
Show file tree
Hide file tree
Showing 16 changed files with 22,306 additions and 11,966 deletions.
25 changes: 10 additions & 15 deletions packages/core/types/css-util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export type CSS<
Theme = {},
ThemeMap = Config.DefaultThemeMap,
Utils = {},
Flat = false
> = (
// nested at-rule css styles
& {
Expand Down Expand Up @@ -90,20 +89,16 @@ export type CSS<
)
}
// unknown css declaration styles
& (
true extends Flat
? Record<never, never>
: {
/** Unknown property. */
[K: string]: (
| number
| string
| CSS<Media, Theme, ThemeMap, Utils>
| {}
| undefined
)
}
)
& {
/** Unknown property. */
[K: string]: (
| number
| string
| CSS<Media, Theme, ThemeMap, Utils>
| {}
| undefined
)
}
)

/** Unique symbol used to reference a property value. */
Expand Down
3 changes: 1 addition & 2 deletions packages/core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export type CSS<
Config['media'],
Config['theme'],
Config['themeMap'],
Config['utils'],
false
Config['utils']
>

/** Returns the properties, attributes, and children expected by a component. */
Expand Down
18 changes: 11 additions & 7 deletions packages/core/types/stitches.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import type * as StyledComponent from './styled-component'
import type * as ThemeUtil from './theme'
import type * as Util from './util'

/** Remove an index signature from a type */
export type RemoveIndex<T> = {[k in keyof T as string extends k ? never : number extends k ? never : k]: T[k]}

/** Stitches interface. */
export default interface Stitches<
Prefix extends string = '',
Expand Down Expand Up @@ -124,21 +127,22 @@ export default interface Stitches<
| string
| Util.Function
| { [name: string]: unknown }
)[]
)[],
CSS = CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
>(
...composers: {
[K in keyof Composers]: (
// Strings and Functions can be skipped over
Composers[K] extends string | Util.Function
? Composers[K]
: CSSUtil.CSS<Media, Theme, ThemeMap, Utils, true> & {
: RemoveIndex<CSS> & {
/** The **variants** property lets you set a subclass of styles based on a key-value pair.
*
* [Read Documentation](https://stitches.dev/docs/variants)
*/
variants?: {
[Name in string]: {
[Pair in number | string]: CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
[Pair in number | string]: CSS
}
}
/** The **variants** property lets you to set a subclass of styles based on a combination of active variants.
Expand All @@ -154,7 +158,7 @@ export default interface Stitches<
: Util.WideObject
)
& {
css: CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
css: CSS
}
)[]
/** The **defaultVariants** property allows you to predefine the active key-value pairs of variants.
Expand All @@ -171,8 +175,8 @@ export default interface Stitches<
} & {
[K2 in keyof Composers[K]]: K2 extends 'compoundVariants' | 'defaultVariants' | 'variants'
? unknown
: K2 extends keyof CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
? CSSUtil.CSS<Media, Theme, ThemeMap, Utils>[K2]
: K2 extends keyof CSS
? CSS[K2]
: unknown
}
)
Expand All @@ -181,7 +185,7 @@ export default interface Stitches<
StyledComponent.StyledComponentType<Composers>,
StyledComponent.StyledComponentProps<Composers>,
Media,
CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
CSS
>
}
}
Expand Down
25 changes: 10 additions & 15 deletions packages/react/types/css-util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export type CSS<
Theme = {},
ThemeMap = Config.DefaultThemeMap,
Utils = {},
Flat = false
> = (
// nested at-rule css styles
& {
Expand Down Expand Up @@ -90,20 +89,16 @@ export type CSS<
)
}
// unknown css declaration styles
& (
true extends Flat
? Record<never, never>
: {
/** Unknown property. */
[K: string]: (
| number
| string
| CSS<Media, Theme, ThemeMap, Utils>
| {}
| undefined
)
}
)
& {
/** Unknown property. */
[K: string]: (
| number
| string
| CSS<Media, Theme, ThemeMap, Utils>
| {}
| undefined
)
}
)

/** Unique symbol used to reference a property value. */
Expand Down
3 changes: 1 addition & 2 deletions packages/react/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export type CSS<
Config['media'],
Config['theme'],
Config['themeMap'],
Config['utils'],
false
Config['utils']
>

/** Returns the properties, attributes, and children expected by a component. */
Expand Down
31 changes: 18 additions & 13 deletions packages/react/types/stitches.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import type * as StyledComponent from './styled-component'
import type * as ThemeUtil from './theme'
import type * as Util from './util'

/** Remove an index signature from a type */
export type RemoveIndex<T> = {[k in keyof T as string extends k ? never : number extends k ? never : k]: T[k]}

/** Stitches interface. */
export default interface Stitches<
Prefix extends string = '',
Expand Down Expand Up @@ -126,21 +129,22 @@ export default interface Stitches<
| React.JSXElementConstructor<any>
| Util.Function
| { [name: string]: unknown }
)[]
)[],
CSS = CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
>(
...composers: {
[K in keyof Composers]: (
// Strings, React Components, and Functions can be skipped over
Composers[K] extends string | React.ExoticComponent<any> | React.JSXElementConstructor<any> | Util.Function
? Composers[K]
: CSSUtil.CSS<Media, Theme, ThemeMap, Utils, true> & {
: RemoveIndex<CSS> & {
/** The **variants** property lets you set a subclass of styles based on a key-value pair.
*
* [Read Documentation](https://stitches.dev/docs/variants)
*/
variants?: {
[Name in string]: {
[Pair in number | string]: CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
[Pair in number | string]: CSS
}
}
/** The **variants** property lets you to set a subclass of styles based on a combination of active variants.
Expand All @@ -156,7 +160,7 @@ export default interface Stitches<
: Util.WideObject
)
& {
css: CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
css: CSS
}
)[]
/** The **defaultVariants** property allows you to predefine the active key-value pairs of variants.
Expand All @@ -173,8 +177,8 @@ export default interface Stitches<
} & {
[K2 in keyof Composers[K]]: K2 extends 'compoundVariants' | 'defaultVariants' | 'variants'
? unknown
: K2 extends keyof CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
? CSSUtil.CSS<Media, Theme, ThemeMap, Utils>[K2]
: K2 extends keyof CSS
? CSS[K2]
: unknown
}
)
Expand All @@ -183,7 +187,7 @@ export default interface Stitches<
StyledComponent.StyledComponentType<Composers>,
StyledComponent.StyledComponentProps<Composers>,
Media,
CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
CSS
>
}
styled: {
Expand All @@ -194,22 +198,23 @@ export default interface Stitches<
| React.ComponentType<any>
| Util.Function
| { [name: string]: unknown }
)[]
)[],
CSS = CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
>(
type: Type,
...composers: {
[K in keyof Composers]: (
// Strings, React Components, and Functions can be skipped over
Composers[K] extends string | React.ComponentType<any> | Util.Function
? Composers[K]
: CSSUtil.CSS<Media, Theme, ThemeMap, Utils, true> & {
: RemoveIndex<CSS> & {
/** The **variants** property lets you set a subclass of styles based on a key-value pair.
*
* [Read Documentation](https://stitches.dev/docs/variants)
*/
variants?: {
[Name in string]: {
[Pair in number | string]: CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
[Pair in number | string]: CSS
}
}
/** The **variants** property lets you to set a subclass of styles based on a combination of active variants.
Expand All @@ -225,7 +230,7 @@ export default interface Stitches<
: Util.WideObject
)
& {
css: CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
css: CSS
}
)[]
/** The **defaultVariants** property allows you to predefine the active key-value pairs of variants.
Expand All @@ -242,8 +247,8 @@ export default interface Stitches<
} & {
[K2 in keyof Composers[K]]: K2 extends 'compoundVariants' | 'defaultVariants' | 'variants'
? unknown
: K2 extends keyof CSSUtil.CSS<Media, Theme, ThemeMap, Utils>
? CSSUtil.CSS<Media, Theme, ThemeMap, Utils>[K2]
: K2 extends keyof CSS
? CSS[K2]
: unknown
}
)
Expand Down
45 changes: 45 additions & 0 deletions packages/test/built-types/built-types-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as React from 'react'
import { styled, CSS } from './stitches.config'

export const Text = styled('span', {
background: '$red100',
paddingBlock: '$2',
color: 'AliceBlue',
variants: {
variant: {
red: {
backgroundColor: '$red100',
}
}
},
})

const DEFAULT_TAG = 'h1'
const bla: CSS = {
background: '$amber1',
}
const Test = styled('div', bla)
export type HeadingProps = React.ComponentProps<typeof DEFAULT_TAG> & { css?: CSS }

// ref should be span element
type ref = React.ElementRef<typeof Text>

const Heading = React.forwardRef<React.ElementRef<typeof DEFAULT_TAG>, HeadingProps>((props, forwardedRef) => {
return (
<>
<Text onClick={(e) => { console.log(e.altKey) }} css={{backgroundClip: 'content-box', "@bp10": { backgroundClip: 'content-box' }}} />
<Text as={DEFAULT_TAG} onClick={(e: any) => { console.log(e.altKey) }} ref={forwardedRef} css={{ borderTopColor: 'AliceBlue', backgroundColor: 'ActiveText', backgroundAttachment: 'inherit', backgroundOrigin: 'border-box', colorAdjust: 'economy' }} />
<Text onClick={(e) => { console.log(e.altKey) }} ref={forwardedRef} css={{ 'backgroundClip': 'border-box', 'ml': 'auto', '@bp1': { backgroundAttachment: 'fixed', }, hello: { backgroundClip: 'border-box' } }} />
<Text onClick={(e) => { console.log(e.altKey) }} ref={forwardedRef} css={{ backgroundColor: '$red100', backgroundAttachment: 'inherit', backgroundOrigin: 'border-box' }} />
<Text as="b" onClick={(e: any) => { console.log(e.altKey) }} ref={forwardedRef} css={{ ...props.css, 'backgroundColor': '$red100', 'padding': 'initial', '@bp3': { paddingBlock: 'inherit', }, }} />
<Text as="div" onClick={(e: any) => { console.log(e.altKey) }} css={{ ...props.css, backgroundColor: '$red100', background: 'red', paddingLeft: 'initial'}} />
<Text as="div" onClick={(e: any) => { console.log(e.altKey) }} css={props.css} />
<Text as={DEFAULT_TAG} {...props} onClick={(e: any) => { console.log(e.altKey) }} ref={forwardedRef} />
</>
)
})

const App = () => {
// when consuming the component it should be very fast too
return <Heading css={{ backgroundColor: '$red100', padding: 'inherit', colorScheme: 'dark' }} />
}
Loading

0 comments on commit 7b35658

Please sign in to comment.