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

fix(TS): allow CSSObject on @emotion/styled #1129

Closed
wants to merge 5 commits into from
Closed
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
7 changes: 3 additions & 4 deletions packages/styled-base/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@
* a style of that component.
*/

import { ComponentSelector, Interpolation } from '@emotion/serialize'
import { ComponentSelector, Interpolation, CSSObject } from '@emotion/serialize'
import * as React from 'react'

import { Omit, Overwrapped, PropsOf } from './helper'

export {
ArrayInterpolation,
CSSObject,
FunctionInterpolation,
ObjectInterpolation
} from '@emotion/serialize'

export { ComponentSelector, Interpolation }
export { ComponentSelector, Interpolation, CSSObject }

type JSXInEl = JSX.IntrinsicElements

Expand Down Expand Up @@ -72,7 +71,7 @@ export interface CreateStyledComponentBase<
ReactClassPropKeys
> = Omit<InnerProps & ExtraProps, ReactClassPropKeys>
>(
template: TemplateStringsArray,
template: TemplateStringsArray | CSSObject,
Copy link
Member

@Ailrun Ailrun Dec 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is covered by the previous overloading, so you don't need to add one.
Interpolation<WithTheme<StyleProps, Theme>> is a (structural) supertype of CSSObject.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... I wonder why I was seeing issues then. I'll dig a little more. Thank you!

...styles: Array<Interpolation<WithTheme<StyleProps, Theme>>>
): StyledComponent<InnerProps, StyleProps, Theme>
}
Expand Down
5 changes: 5 additions & 0 deletions packages/styled-base/types/tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ const Button1 = styled('button')({
<Button1 type="button" />
</div>

// $ExpectError
const BadButton0 = styled('button')({ bad: 'value' })
// $ExpectError
const BadButton1 = styled('button')(() => ({ bad: 'value' }))

const Input0 = styled('input', {
label: 'mystyle'
})`
Expand Down
28 changes: 28 additions & 0 deletions packages/styled/types/tests.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as React from 'react'
import styled from '@emotion/styled'

let ui: React.ReactElement<any>

// $ExpectType CreateStyledComponentIntrinsic<"a", {}, any>
styled.a
// $ExpectType CreateStyledComponentIntrinsic<"body", {}, any>
Expand All @@ -8,3 +11,28 @@ styled.body
styled.div
// $ExpectType CreateStyledComponentIntrinsic<"svg", {}, any>
styled.svg

const Button1 = styled.button({
color: 'blue'
})
ui = (
<div>
<Button1 />
<Button1 type="button" />
</div>
)

const Button2 = styled.button<{ size?: 'md' | 'sm' }>(
({ size }) => (size === 'md' ? { fontSize: 15 } : { fontSize: 10 })
)
ui = (
<div>
<Button2 />
<Button2 size="sm" />
</div>
)

// $ExpectError
const BadButton0 = styled.button({ bad: 'value' })
// $ExpectError
const BadButton1 = styled.button(() => ({ bad: 'value' }))