Skip to content

Commit c9b84db

Browse files
Fix mismatch between CSSInterpolation and Interpolation<Props> (#3164)
* Add explicit test for uncovered regression; fix interpolation type to make it pass * Reorder overloads for styled component creator Formerly, the first overload to be tried was not accepting template strings array as first argument. Therefore, it couldn't be used when `styled` was used as a tag for template string. So in this case TS skipped this overload and fell through to the next. Now, though, with `ArrayInterpolation` type changed, `TemplateStringsArray` matches the definition of `ArrayInterpolation`; therefore, this overload becomes used for template strings, confusing type inference. This change moves this overload to the end of the list, i.e. to be used as fallback when there's actually a direct function call, without template string. * yarn changeset * Apply suggestions from code review * Create tiny-snails-watch.md --------- Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
1 parent 6e0e388 commit c9b84db

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

.changeset/curly-planets-search.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@emotion/serialize': patch
3+
---
4+
5+
Make `ArrayInterpolation` to extend `ReadonlyArray` to match a similar recent change to `ArrayCSSInterpolation`. It fixes some compatibility issues when those 2 get mixed together.

.changeset/tiny-snails-watch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@emotion/styled": patch
3+
---
4+
5+
Reordered `styled` overloads to accommodate the recent change in `@emotion/serialize`'s types.

packages/react/types/tests.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
withEmotionCache
1010
} from '@emotion/react'
1111
import { JSX as EmotionJSX } from '@emotion/react/jsx-runtime'
12+
import { CSSInterpolation } from '@emotion/serialize'
1213

1314
declare module '@emotion/react' {
1415
// tslint:disable-next-line: strict-export-declare-modifiers
@@ -23,6 +24,9 @@ declare module '@emotion/react' {
2324
;<Global styles={[]} />
2425
;<Global styles={theme => [theme.primaryColor]} />
2526

27+
declare const getStyles: () => CSSInterpolation
28+
;<Global styles={getStyles()} />
29+
2630
declare const getRandomColor: () => string
2731

2832
const ComponentWithCache = withEmotionCache((_props: {}, cache) => {

packages/serialize/types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export type Keyframes = {
5252
} & string
5353

5454
export interface ArrayInterpolation<Props>
55-
extends Array<Interpolation<Props>> {}
55+
extends ReadonlyArray<Interpolation<Props>> {}
5656

5757
export interface FunctionInterpolation<Props> {
5858
(props: Props): Interpolation<Props>

packages/styled/types/base.d.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ export interface CreateStyledComponent<
6363
SpecificComponentProps extends {} = {},
6464
JSXProps extends {} = {}
6565
> {
66+
(
67+
template: TemplateStringsArray,
68+
...styles: Array<
69+
Interpolation<ComponentProps & SpecificComponentProps & { theme: Theme }>
70+
>
71+
): StyledComponent<ComponentProps, SpecificComponentProps, JSXProps>
72+
6673
/**
6774
* @typeparam AdditionalProps Additional props to add to your styled component
6875
*/
69-
<AdditionalProps extends {} = {}>(
76+
<AdditionalProps extends {}>(
77+
template: TemplateStringsArray,
7078
...styles: Array<
7179
Interpolation<
7280
ComponentProps &
@@ -80,18 +88,10 @@ export interface CreateStyledComponent<
8088
JSXProps
8189
>
8290

83-
(
84-
template: TemplateStringsArray,
85-
...styles: Array<
86-
Interpolation<ComponentProps & SpecificComponentProps & { theme: Theme }>
87-
>
88-
): StyledComponent<ComponentProps, SpecificComponentProps, JSXProps>
89-
9091
/**
9192
* @typeparam AdditionalProps Additional props to add to your styled component
9293
*/
93-
<AdditionalProps extends {}>(
94-
template: TemplateStringsArray,
94+
<AdditionalProps extends {} = {}>(
9595
...styles: Array<
9696
Interpolation<
9797
ComponentProps &

0 commit comments

Comments
 (0)