-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathindex.d.ts
151 lines (134 loc) Β· 4.63 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Definitions by: Junyoung Clare Jang <https://github.com/Ailrun>
// TypeScript Version: 3.1
/**
* @desc
* In following types,
* `InnerProps` is type parameter that represents props type of
* internal component (target of styling)
* `ExtraProps` is type parameter that represents extra props type of
* styled component.
* `StyleProps` is type parameter that represents props used in
* a style of that component.
*/
import { ComponentSelector, Interpolation } 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 }
type JSXInEl = JSX.IntrinsicElements
export type WithTheme<P, T> = P extends { theme: infer Theme }
? P & { theme: Exclude<Theme, undefined> }
: P & { theme: T }
export interface StyledOptions {
label?: string
shouldForwardProp?(propName: string): boolean
target?: string
}
export interface StyledComponent<InnerProps, StyleProps, Theme extends object>
extends React.SFC<InnerProps & Omit<StyleProps, 'theme'> & { theme?: Theme }>,
ComponentSelector {
/**
* @desc this method is type-unsafe
*/
withComponent<NewTag extends keyof JSXInEl>(
tag: NewTag
): StyledComponent<JSXInEl[NewTag], StyleProps, Theme>
withComponent<Tag extends React.ComponentType<any>>(
tag: Tag
): StyledComponent<PropsOf<Tag>, StyleProps, Theme>
}
type ReactClassPropKeys = keyof React.ClassAttributes<any>
interface CreateStyledComponentBaseThemeless<InnerProps, ExtraProps> {
<
StyleProps extends Omit<
Overwrapped<InnerProps, StyleProps>,
ReactClassPropKeys
> = Omit<InnerProps & ExtraProps, ReactClassPropKeys>,
Theme extends object = object
>(
...styles: Array<Interpolation<WithTheme<StyleProps, Theme>>>
): StyledComponent<InnerProps, StyleProps, Theme>
<
StyleProps extends Omit<
Overwrapped<InnerProps, StyleProps>,
ReactClassPropKeys
> = Omit<InnerProps & ExtraProps, ReactClassPropKeys>,
Theme extends object = object
>(
template: TemplateStringsArray,
...styles: Array<Interpolation<WithTheme<StyleProps, Theme>>>
): StyledComponent<InnerProps, StyleProps, Theme>
}
interface CreateStyledComponentBaseThemed<
InnerProps,
ExtraProps,
StyledInstanceTheme extends object
> {
<
StyleProps extends Omit<
Overwrapped<InnerProps, StyleProps>,
ReactClassPropKeys
> = Omit<InnerProps & ExtraProps, ReactClassPropKeys>
>(
...styles: Array<Interpolation<WithTheme<StyleProps, StyledInstanceTheme>>>
): StyledComponent<InnerProps, StyleProps, StyledInstanceTheme>
<
StyleProps extends Omit<
Overwrapped<InnerProps, StyleProps>,
ReactClassPropKeys
> = Omit<InnerProps & ExtraProps, ReactClassPropKeys>
>(
template: TemplateStringsArray,
...styles: Array<Interpolation<WithTheme<StyleProps, StyledInstanceTheme>>>
): StyledComponent<InnerProps, StyleProps, StyledInstanceTheme>
}
export type CreateStyledComponentBase<
InnerProps,
ExtraProps,
StyledInstanceTheme extends object
> =
// this "reversed" condition checks if StyledInstanceTheme was already parametrized when using CreateStyled
object extends StyledInstanceTheme
? CreateStyledComponentBaseThemeless<InnerProps, ExtraProps>
: CreateStyledComponentBaseThemed<
InnerProps,
ExtraProps,
StyledInstanceTheme
>
export type CreateStyledComponentIntrinsic<
Tag extends keyof JSXInEl,
ExtraProps,
Theme extends object
> = CreateStyledComponentBase<JSXInEl[Tag], ExtraProps, Theme>
export type CreateStyledComponentExtrinsic<
Tag extends React.ComponentType<any>,
ExtraProps,
Theme extends object
> = CreateStyledComponentBase<PropsOf<Tag>, ExtraProps, Theme>
/**
* @desc
* This function accepts `InnerProps`/`Tag` to infer the type of `tag`,
* and accepts `ExtraProps` for user who use string style
* to be able to declare extra props without using
* `` styled('button')<ExtraProps>`...` ``, which does not supported in
* styled-component VSCode extension.
* If your tool support syntax highlighting for `` styled('button')<ExtraProps>`...` ``
* it could be more efficient.
*/
export interface CreateStyled<Theme extends object = any> {
<Tag extends React.ComponentType<any>, ExtraProps = {}>(
tag: Tag,
options?: StyledOptions
): CreateStyledComponentExtrinsic<Tag, ExtraProps, Theme>
<Tag extends keyof JSXInEl, ExtraProps = {}>(
tag: Tag,
options?: StyledOptions
): CreateStyledComponentIntrinsic<Tag, ExtraProps, Theme>
}
declare const styled: CreateStyled
export default styled