/
styled.ts
186 lines (142 loc) · 4.56 KB
/
styled.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* tslint:disable:no-unnecessary-generics */
// eslint-disable-next-line import/no-extraneous-dependencies
import * as React from 'react';
import { css } from '@linaria/core';
import { styled } from '../src';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function isExtends<C, T>(arg1?: C, arg2?: T): C extends T ? 'extends' : never {
// It will never be executed, so the result doesn't matter.
return null as any;
}
const Fabric =
<T>(): React.FC<T> =>
(props) =>
React.createElement('div', props);
const Header = (p: { children: string }) => React.createElement('h1', p);
const Generic = <T>(
p: T & { className?: string; style?: React.CSSProperties }
) => React.createElement('h1', p);
const StyledDiv = styled.div``;
// $ExpectType "extends"
isExtends<typeof StyledDiv, React.FC<React.DetailedHTMLProps<any, any>>>();
const A = (): React.ReactElement => React.createElement('div', null);
// $ExpectError
styled(A)``;
// foo is not a valid property of div
// $ExpectError
React.createElement(StyledDiv, { foo: 'foo' });
const ReStyledDiv = styled(StyledDiv)<{ foo: string }>``;
React.createElement(ReStyledDiv, { foo: 'foo' });
// component should have className property
// $ExpectError
styled(Fabric<{ a: string }>())``;
// className property should be string
// $ExpectError
styled(Fabric<{ className: number }>())``;
const SimplestComponent = styled(Fabric<{ className: string }>())``;
// $ExpectType "extends"
isExtends<typeof SimplestComponent, React.FC<{ className: string }>>();
styled(Fabric<{ className: string }>())`
// component should have style property
// $ExpectError
color: ${() => 'red'};
`;
styled(Fabric<{ className: string }>())`
// it looks like function, but it's a reference to another styled component
& > ${SimplestComponent} {
color: red;
}
`;
styled(Fabric<{ className: string }>())`
// it looks like the previous test, but it references a non-linaria component
// $ExpectError
& > ${Header} {
color: red;
}
`;
styled(Fabric<{ className: string; style: {} }>())`
color: ${() => 'red'};
`;
styled(Fabric<{ className: string; style: {} }>())`
// color should be defined in props
// $ExpectError
color: ${(props) => props.color};
`;
styled(Fabric<{ className: string; style: {}; color: 'red' | 'blue' }>())`
& > ${SimplestComponent} {
color: ${(props) => props.color};
}
`;
// $ExpectType number
Generic({ children: 123 }).props.children;
const StyledGeneric = styled(Generic)``;
// $ExpectType number
StyledGeneric({ children: 123 }).props.children;
styled.a`
& > ${SimplestComponent} {
color: red;
}
`({ href: 'about:blank' });
((/* Issue #536 */) => {
const Title = styled.div<{ background: string }>`
background: ${(props) => props.background};
`;
// $ExpectType "extends"
isExtends<typeof Title, React.FC<{ background: string }>>();
css`
${Title} {
color: green;
}
`;
})();
((/* Issue #622 */) => {
const Wrapper = styled.div<{ prop1: boolean }>`
width: 1em;
background-color: ${(props) => (props.prop1 ? 'transparent' : 'green')};
`;
const Custom: React.FC<{ className?: string; id: number }> = () => null;
const tag = styled(Custom);
const Card = tag`
${Wrapper} {
color: green;
}
`;
// $ExpectType Validator<number> | undefined
Card.propTypes!.id;
const styledTag = styled(Wrapper);
const NewWrapper = styledTag<{ prop2: string }>`
width: 2em;
background-color: ${(props) => (props.prop1 ? 'transparent' : 'red')};
color: ${(props) => props.prop2};
`;
// $ExpectType Validator<boolean> | undefined
NewWrapper.propTypes!.prop1;
// $ExpectType Validator<string> | undefined
NewWrapper.propTypes!.prop2;
})();
((/* Issue #844 */) => {
type GridProps = { container?: false } | { container: true; spacing: number };
const Grid: React.FC<GridProps & { className?: string }> = () => null;
// Type 'false' is not assignable to type 'true'
// $ExpectError
React.createElement(Grid, { container: false, spacing: 8 });
React.createElement(Grid, { container: true, spacing: 8 });
styled(Grid)``;
})();
((/* Issue #872 */) => {
interface BaseProps {
className?: string;
style?: React.CSSProperties;
}
interface ComponentProps extends BaseProps {
title: string;
}
const Flow = <TProps extends BaseProps>(Cmp: React.FC<TProps>) =>
styled(Cmp)`
display: flow;
`;
const Component: React.FC<ComponentProps> = (props) =>
React.createElement('div', props);
const Implementation = Flow(Component);
(() => React.createElement(Implementation, { title: 'Title' }))();
})();