1- import * as React from 'react'
2- import {
3- getDefaultShouldForwardProp ,
4- composeShouldForwardProps
5- /*
6- type StyledOptions,
7- type CreateStyled,
8- type PrivateStyledComponent,
9- type StyledElementType
10- */
11- } from './utils'
12- import { withEmotionCache , ThemeContext } from '@emotion/react'
13- import isDevelopment from '#is-development'
141import isBrowser from '#is-browser'
2+ import isDevelopment from '#is-development'
3+ import { Theme , ThemeContext , withEmotionCache } from '@emotion/react'
4+ import { Interpolation , serializeStyles } from '@emotion/serialize'
5+ import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
156import {
7+ EmotionCache ,
168 getRegisteredStyles ,
179 insertStyles ,
18- registerStyles
10+ registerStyles ,
11+ SerializedStyles
1912} from '@emotion/utils'
20- import { serializeStyles } from '@emotion/serialize'
21- import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
13+ import * as React from 'react'
14+ import { CreateStyled , ElementType , StyledOptions } from './types'
15+ import { composeShouldForwardProps , getDefaultShouldForwardProp } from './utils'
16+ export type {
17+ ArrayInterpolation ,
18+ ComponentSelector ,
19+ CSSObject ,
20+ FunctionInterpolation ,
21+ Interpolation
22+ } from '@emotion/serialize'
2223
2324const ILLEGAL_ESCAPE_SEQUENCE_ERROR = `You have illegal escape sequence in your template literal, most likely inside content's property value.
2425Because you write your CSS inside a JavaScript string you actually have to do double escaping, so for example "content: '\\00d7';" should become "content: '\\\\00d7';".
2526You can read more about this here:
2627https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#ES2018_revision_of_illegal_escape_sequences`
2728
28- const Insertion = ( { cache, serialized, isStringTag } ) => {
29+ const Insertion = ( {
30+ cache,
31+ serialized,
32+ isStringTag
33+ } : {
34+ cache : EmotionCache
35+ serialized : SerializedStyles
36+ isStringTag : boolean
37+ } ) => {
2938 registerStyles ( cache , serialized , isStringTag )
3039
3140 const rules = useInsertionEffectAlwaysWithSyncFallback ( ( ) =>
@@ -52,10 +61,7 @@ const Insertion = ({ cache, serialized, isStringTag }) => {
5261 return null
5362}
5463
55- let createStyled /*: CreateStyled */ = (
56- tag /*: any */ ,
57- options /* ?: StyledOptions */
58- ) => {
64+ const createStyled = ( tag : ElementType , options ?: StyledOptions ) => {
5965 if ( isDevelopment ) {
6066 if ( tag === undefined ) {
6167 throw new Error (
@@ -66,8 +72,8 @@ let createStyled /*: CreateStyled */ = (
6672 const isReal = tag . __emotion_real === tag
6773 const baseTag = ( isReal && tag . __emotion_base ) || tag
6874
69- let identifierName
70- let targetClassName
75+ let identifierName : string | undefined
76+ let targetClassName : string | undefined
7177 if ( options !== undefined ) {
7278 identifierName = options . label
7379 targetClassName = options . target
@@ -78,9 +84,11 @@ let createStyled /*: CreateStyled */ = (
7884 shouldForwardProp || getDefaultShouldForwardProp ( baseTag )
7985 const shouldUseAs = ! defaultShouldForwardProp ( 'as' )
8086
81- /* return function<Props>(): PrivateStyledComponent<Props> { */
8287 return function ( ) {
83- let args = arguments
88+ // eslint-disable-next-line prefer-rest-params
89+ let args = arguments as any as Array <
90+ TemplateStringsArray | Interpolation < Theme >
91+ >
8492 let styles =
8593 isReal && tag . __emotion_styles !== undefined
8694 ? tag . __emotion_styles . slice ( 0 )
@@ -89,29 +97,35 @@ let createStyled /*: CreateStyled */ = (
8997 if ( identifierName !== undefined ) {
9098 styles . push ( `label:${ identifierName } ;` )
9199 }
92- if ( args [ 0 ] == null || args [ 0 ] . raw === undefined ) {
100+ if (
101+ args [ 0 ] == null ||
102+ ( args [ 0 ] as TemplateStringsArray ) . raw === undefined
103+ ) {
104+ // eslint-disable-next-line prefer-spread
93105 styles . push . apply ( styles , args )
94106 } else {
95- if ( isDevelopment && args [ 0 ] [ 0 ] === undefined ) {
107+ const templateStringsArr = args [ 0 ] as TemplateStringsArray
108+ if ( isDevelopment && templateStringsArr [ 0 ] === undefined ) {
96109 console . error ( ILLEGAL_ESCAPE_SEQUENCE_ERROR )
97110 }
98- styles . push ( args [ 0 ] [ 0 ] )
111+ styles . push ( templateStringsArr [ 0 ] )
99112 let len = args . length
100113 let i = 1
101114 for ( ; i < len ; i ++ ) {
102- if ( isDevelopment && args [ 0 ] [ i ] === undefined ) {
115+ if ( isDevelopment && templateStringsArr [ i ] === undefined ) {
103116 console . error ( ILLEGAL_ESCAPE_SEQUENCE_ERROR )
104117 }
105- styles . push ( args [ i ] , args [ 0 ] [ i ] )
118+ styles . push ( args [ i ] , templateStringsArr [ i ] )
106119 }
107120 }
108121
109- const Styled /*: PrivateStyledComponent<Props> */ = withEmotionCache (
110- ( props , cache , ref ) => {
111- const FinalTag = ( shouldUseAs && props . as ) || baseTag
122+ const Styled : ElementType = withEmotionCache (
123+ ( props : Record < string , unknown > , cache , ref ) => {
124+ const FinalTag =
125+ ( shouldUseAs && ( props . as as React . ElementType ) ) || baseTag
112126
113127 let className = ''
114- let classInterpolations = [ ]
128+ let classInterpolations : Interpolation < Theme > [ ] = [ ]
115129 let mergedProps = props
116130 if ( props . theme == null ) {
117131 mergedProps = { }
@@ -146,7 +160,7 @@ let createStyled /*: CreateStyled */ = (
146160 ? getDefaultShouldForwardProp ( FinalTag )
147161 : defaultShouldForwardProp
148162
149- let newProps = { }
163+ let newProps : Record < string , unknown > = { }
150164
151165 for ( let key in props ) {
152166 if ( shouldUseAs && key === 'as' ) continue
@@ -196,20 +210,20 @@ let createStyled /*: CreateStyled */ = (
196210 return `.${ targetClassName } `
197211 }
198212 } )
199-
200- Styled . withComponent = (
201- nextTag /*: StyledElementType<Props> */ ,
202- nextOptions /* ?: StyledOptions */
213+ ; ( Styled as any ) . withComponent = (
214+ nextTag : ElementType ,
215+ nextOptions : StyledOptions
203216 ) => {
204- return createStyled ( nextTag , {
217+ const newStyled = createStyled ( nextTag , {
205218 ...options ,
206219 ...nextOptions ,
207220 shouldForwardProp : composeShouldForwardProps ( Styled , nextOptions , true )
208- } ) ( ...styles )
221+ } )
222+ return ( newStyled as any ) ( ...styles )
209223 }
210224
211225 return Styled
212226 }
213227}
214228
215- export default createStyled
229+ export default createStyled as CreateStyled
0 commit comments