forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionalComponent.test-d.tsx
76 lines (65 loc) · 1.61 KB
/
functionalComponent.test-d.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
import {
h,
Text,
FunctionalComponent,
expectError,
expectType,
Component
} from './index'
// simple function signature
const Foo = (props: { foo: number }) => h(Text, null, props.foo)
// TSX
expectType<JSX.Element>(<Foo foo={1} />)
expectType<JSX.Element>(<Foo foo={1} key="1" />)
expectType<JSX.Element>(<Foo foo={1} ref="ref" />)
// @ts-expect-error
expectError(<Foo />)
// @ts-expect-error
expectError(<Foo foo="bar" />)
// @ts-expect-error
expectError(<Foo baz="bar" />)
// Explicit signature with props + emits
const Bar: FunctionalComponent<
{ foo: number },
{ update: (value: number) => void }
> = (props, { emit }) => {
expectType<number>(props.foo)
emit('update', 123)
// @ts-expect-error
expectError(emit('nope'))
// @ts-expect-error
expectError(emit('update'))
// @ts-expect-error
expectError(emit('update', 'nope'))
}
// assigning runtime options
Bar.props = {
foo: Number
}
// @ts-expect-error
expectError((Bar.props = { foo: String }))
Bar.emits = {
update: value => value > 1
}
// @ts-expect-error
expectError((Bar.emits = { baz: () => void 0 }))
// TSX
expectType<JSX.Element>(<Bar foo={1} />)
// @ts-expect-error
expectError(<Foo />)
// @ts-expect-error
expectError(<Bar foo="bar" />)
// @ts-expect-error
expectError(<Foo baz="bar" />)
const Baz: FunctionalComponent<{}, string[]> = (props, { emit }) => {
expectType<{}>(props)
expectType<(event: string) => void>(emit)
}
expectType<Component>(Baz)
const Qux: FunctionalComponent<{}, ['foo', 'bar']> = (props, { emit }) => {
emit('foo')
emit('foo', 1, 2)
emit('bar')
emit('bar', 1, 2)
}
expectType<Component>(Qux)