-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
jsx-types.unit.tsx
333 lines (309 loc) · 11.5 KB
/
jsx-types.unit.tsx
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { assertType, describe, expectTypeOf, test } from 'vitest';
import { $, type PropFunction } from '../../../qrl/qrl.public';
import type { EventHandler, QRLEventHandlerMulti } from './jsx-qwik-attributes';
import type { FunctionComponent, JSXOutput } from './jsx-node';
import type { QwikIntrinsicElements } from './jsx-qwik-elements';
import type { JSXChildren } from './jsx-qwik-attributes';
import { component$, type PropsOf, type PublicProps } from '../../../component/component.public';
import type { QwikHTMLElements, QwikSVGElements, Size } from './jsx-generated';
import type { JSX } from '../jsx-runtime';
describe('types', () => {
// Note, these type checks happen at compile time. We don't need to call anything, so we do ()=>()=>. We just need to
// make sure the type check runs.
test('basic', () => () => {
expectTypeOf(<div />).toEqualTypeOf<JSXOutput>();
expectTypeOf<QRLEventHandlerMulti<PointerEvent, HTMLDivElement>>().toMatchTypeOf<
QwikIntrinsicElements['div']['onAuxClick$']
>();
expectTypeOf<QwikIntrinsicElements['li']['children']>().toEqualTypeOf<JSXChildren>();
expectTypeOf<QwikIntrinsicElements['link']['children']>().toEqualTypeOf<undefined>();
expectTypeOf<QwikIntrinsicElements['svg']['width']>().toEqualTypeOf<Size | undefined>();
});
test('untyped components', () => () => {
const WithP = component$((p) => {
expectTypeOf(p).toEqualTypeOf<unknown>();
return <div />;
});
const NoP = component$(() => <div />);
expectTypeOf<PropsOf<typeof WithP>>().toEqualTypeOf<never>();
expectTypeOf<PropsOf<typeof NoP>>().toEqualTypeOf<never>();
component$(() => {
return (
<>
<WithP key={123}>
<div />
</WithP>
<NoP key={123}>
<div />
</NoP>
</>
);
});
const Fn = () => <div />;
expectTypeOf<PropsOf<typeof Fn>>().toEqualTypeOf<never>();
const Cmp = component$(Fn);
expectTypeOf(Cmp).toEqualTypeOf<FunctionComponent<PublicProps<unknown>>>();
expectTypeOf(Cmp).not.toEqualTypeOf<FunctionComponent<{ foo: string }>>();
});
test('inferring FunctionComponent', () => () => {
const makeFC = <T,>(fn: (p: T) => JSX.Element): FunctionComponent<T> => fn;
const FCNoP = makeFC(() => <div />);
expectTypeOf<PropsOf<typeof FCNoP>>().toEqualTypeOf<never>();
const FCWithP = makeFC((p) => {
expectTypeOf(p).not.toBeAny();
expectTypeOf(p).toEqualTypeOf<unknown>();
return <div />;
});
expectTypeOf<PropsOf<typeof FCWithP>>().toEqualTypeOf<never>();
});
test('accepting FunctionComponent', () => () => {
const f = <P,>(fn: FunctionComponent<P>) => null as P;
expectTypeOf(f(() => <div />)).toEqualTypeOf<unknown>();
expectTypeOf(
f((p) => {
expectTypeOf(p).not.toBeAny();
expectTypeOf(p).toEqualTypeOf<unknown>();
return <div />;
})
).toEqualTypeOf<unknown>();
expectTypeOf(f(component$<{ foo: string }>(() => <div />))).toEqualTypeOf<
PublicProps<{ foo: string }>
>();
});
test('component', () => () => {
const Cmp = component$((props: PropsOf<'svg'>) => {
const { width = '240', height = '56', onClick$, ...rest } = props;
expectTypeOf(onClick$).toEqualTypeOf<QRLEventHandlerMulti<PointerEvent, SVGSVGElement>>();
return (
<svg
xmlns="http://www.w3.org/2000/svg"
version="1.1"
width={width}
height={height}
{...rest}
/>
);
});
expectTypeOf<Parameters<typeof Cmp>[0]['onClick$']>().toMatchTypeOf<
EventHandler<PointerEvent, SVGSVGElement> | QRLEventHandlerMulti<PointerEvent, SVGSVGElement>
>();
return (
<p>
<Cmp />
</p>
);
});
test('PropFunction', () => () => {
const CmpButton = component$<{
onClick$?: PropFunction<() => void>;
}>((props) => <button onClick$={props.onClick$} />);
<CmpButton onClick$={() => alert('CLICKED!')}>click me!</CmpButton>;
});
test('unknown string component', () => () => {
const t = (
<hello-there
class="hi"
onClick$={(ev, el) => {
expectTypeOf(ev).not.toBeAny();
expectTypeOf(ev).toEqualTypeOf<PointerEvent>();
// Because of interface constraints, this type is "never"
// expectTypeOf(el).toEqualTypeOf<Element>();
}}
/>
);
expectTypeOf(t).toEqualTypeOf<JSX.Element>();
});
test('inferring', () => () => {
// Popover API
expectTypeOf<PropsOf<'button'>>().toMatchTypeOf<{
popovertarget?: string;
}>();
expectTypeOf<{
popovertarget?: string;
}>().not.toMatchTypeOf<PropsOf<'input'>>();
expectTypeOf<{
type: 'button';
popovertarget?: string;
}>().toMatchTypeOf<PropsOf<'input'>>();
<>
<button popovertarget="meep" />
<input type="button" popovertarget="meep" />
<div popover="manual" id="meep" />
<div
onToggle$={(ev, el) => {
expectTypeOf(ev).not.toBeAny();
// It's Event because api extractor doesn't know about ToggleEvent
// assertType<ToggleEvent>(ev);
expectTypeOf(ev.newState).toBeString();
}}
onBeforeToggle$={(ev, el) => {
expectTypeOf(ev).not.toBeAny();
// assertType<ToggleEvent>(ev);
expectTypeOf(ev.prevState).toBeString();
}}
onBlur$={(ev) => {
expectTypeOf(ev).not.toBeAny();
assertType<FocusEvent>(ev);
}}
window:onAnimationEnd$={(ev) => {
expectTypeOf(ev).not.toBeAny();
assertType<AnimationEvent>(ev);
}}
document:onAbort$={(ev) => {
expectTypeOf(ev).not.toBeAny();
assertType<UIEvent>(ev);
}}
// Infer through $
onAuxClick$={$((ev) => {
expectTypeOf(ev).not.toBeAny();
assertType<PointerEvent>(ev);
})}
// Array of handlers
onInput$={[
$((ev) => {
expectTypeOf(ev).not.toBeAny();
assertType<InputEvent>(ev);
}),
null,
undefined,
[
$(async (ev, el) => {
expectTypeOf(ev).not.toBeAny();
assertType<InputEvent>(ev);
expectTypeOf(el).not.toBeAny();
assertType<HTMLDivElement>(el);
}),
],
]}
/>
</>;
});
test('polymorphic component', () => () => {
const Poly = component$(
<C extends string | FunctionComponent = 'div'>({
as,
...props
}: { as?: C } & PropsOf<string extends C ? 'div' : C>) => {
const Cmp = as || 'div';
return <Cmp {...props}>hi</Cmp>;
}
);
expectTypeOf<Parameters<typeof Poly<'button'>>[0]['popovertarget']>().toEqualTypeOf<
string | undefined
>();
expectTypeOf<Parameters<typeof Poly<'a'>>[0]['href']>().toEqualTypeOf<string | undefined>();
expectTypeOf<Parameters<typeof Poly<'button'>>[0]>().not.toHaveProperty('href');
expectTypeOf<Parameters<typeof Poly<'a'>>[0]>().not.toHaveProperty('popovertarget');
expectTypeOf<
Parameters<Extract<Parameters<typeof Poly>[0]['onClick$'], EventHandler>>[1]
>().toEqualTypeOf<HTMLDivElement>();
const MyCmp = component$((p: { name: string }) => <span>Hi {p.name}</span>);
return (
<>
<Poly
onClick$={(ev, el) => {
expectTypeOf(ev).not.toBeAny();
expectTypeOf(ev).toEqualTypeOf<PointerEvent>();
expectTypeOf(el).toEqualTypeOf<HTMLDivElement>();
}}
// This should error
// popovertarget
>
Foo
</Poly>
<Poly
as="a"
onClick$={(ev, el) => {
expectTypeOf(ev).not.toBeAny();
expectTypeOf(ev).toEqualTypeOf<PointerEvent>();
expectTypeOf(el).toEqualTypeOf<HTMLAnchorElement>();
}}
href="hi"
// This should error
// popovertarget
>
Foo
</Poly>
<Poly
as="button"
onClick$={(ev, el) => {
expectTypeOf(ev).not.toBeAny();
expectTypeOf(ev).toEqualTypeOf<PointerEvent>();
expectTypeOf(el).toEqualTypeOf<HTMLButtonElement>();
}}
popovertarget="foo"
>
Bar
</Poly>
<Poly as={Poly} />
<Poly as={MyCmp} name="meep" />
</>
);
});
test('FunctionComponent', () => () => {
const Cmp = component$((props: { foo: string }) => null);
expectTypeOf(Cmp).toMatchTypeOf<FunctionComponent<{ foo: string }>>();
expectTypeOf<FunctionComponent<{ foo: string }>>().toMatchTypeOf(Cmp);
expectTypeOf((p: { hi: number }) => <span>{p.hi}</span>).toMatchTypeOf<FunctionComponent>();
expectTypeOf((p: { hi: number }) => <span>{p.hi}</span>).toMatchTypeOf<
FunctionComponent<{ hi: number }>
>();
expectTypeOf((p: { hi: number }) => <span>{p.hi}</span>).not.toMatchTypeOf<
FunctionComponent<{ hi: string }>
>();
expectTypeOf((p: { hi: number }) => <span>{p.hi}</span>).not.toMatchTypeOf<
FunctionComponent<{ meep: string }>
>();
expectTypeOf((p: { hi: number }) => `${p.hi}`).toMatchTypeOf<
FunctionComponent<{ hi: number }>
>();
expectTypeOf((p: { hi: number }) => p.hi).toMatchTypeOf<FunctionComponent<{ hi: number }>>();
expectTypeOf((p: { hi?: number | boolean | null }) => p.hi).toMatchTypeOf<
FunctionComponent<{ hi: number }>
>();
expectTypeOf(() => null).toMatchTypeOf<FunctionComponent<{ hi: number }>>();
expectTypeOf(() => new Date()).not.toMatchTypeOf<FunctionComponent>();
});
test('PropsOf', () => () => {
// tags
expectTypeOf<PropsOf<'div'>>().toEqualTypeOf<QwikHTMLElements['div']>();
expectTypeOf<PropsOf<'div'>>().not.toEqualTypeOf<QwikIntrinsicElements['li']>();
expectTypeOf<PropsOf<'path'>>().toEqualTypeOf<QwikSVGElements['path']>();
expectTypeOf<PropsOf<'not-exist'>>().toEqualTypeOf<QwikHTMLElements['span']>();
// functions
const NoProps = () => <div />;
expectTypeOf<PropsOf<typeof NoProps>>().toEqualTypeOf<never>();
const UnknownProps = (p: unknown) => <div />;
expectTypeOf<PropsOf<typeof UnknownProps>>().toEqualTypeOf<never>();
const AnyProps = (p: any) => <div />;
expectTypeOf<PropsOf<typeof AnyProps>>().toEqualTypeOf<any>();
const DefProps = (props: { foo: string }) => <div />;
expectTypeOf<PropsOf<typeof DefProps>>().toEqualTypeOf<{ foo: string }>();
const PolyProps = <C extends string = ''>(
p: { as?: C; b: boolean } & (C extends 'hi' ? { foo: boolean } : never)
) => <div />;
expectTypeOf<PropsOf<typeof PolyProps<'hi'>>>().toMatchTypeOf<{
as?: 'hi';
b: boolean;
foo: boolean;
}>();
// components
const NoProps$ = component$(NoProps);
expectTypeOf<PropsOf<typeof NoProps$>>().toEqualTypeOf<never>();
const UnknownProps$ = component$(UnknownProps);
expectTypeOf<PropsOf<typeof UnknownProps$>>().toEqualTypeOf<never>();
const AnyProps$ = component$(AnyProps);
expectTypeOf<PropsOf<typeof AnyProps$>>().toEqualTypeOf<any>();
const DefProps$ = component$(DefProps);
expectTypeOf<PropsOf<typeof DefProps$>>().toEqualTypeOf<{ foo: string }>();
const PolyProps$ = component$(PolyProps);
expectTypeOf<PropsOf<typeof PolyProps$<'hi'>>>().toMatchTypeOf<{
as?: 'hi';
b: boolean;
foo: boolean;
}>();
// edge cases
expectTypeOf<PropsOf<typeof DefProps$ | null>>().toEqualTypeOf<{ foo: string }>();
expectTypeOf<PropsOf<17>>().toEqualTypeOf<never>();
});
});