2020-08-19 22:00:48 +08:00
|
|
|
import {
|
2021-03-30 05:38:25 +08:00
|
|
|
h,
|
|
|
|
Text,
|
2020-08-19 22:00:48 +08:00
|
|
|
FunctionalComponent,
|
|
|
|
expectError,
|
|
|
|
expectType,
|
|
|
|
Component
|
|
|
|
} from './index'
|
2020-04-04 00:05:52 +08:00
|
|
|
|
|
|
|
// simple function signature
|
2021-03-30 05:38:25 +08:00
|
|
|
const Foo = (props: { foo: number }) => h(Text, null, props.foo)
|
2020-04-04 00:05:52 +08:00
|
|
|
|
|
|
|
// TSX
|
|
|
|
expectType<JSX.Element>(<Foo foo={1} />)
|
2020-07-07 09:59:26 +08:00
|
|
|
expectType<JSX.Element>(<Foo foo={1} key="1" />)
|
|
|
|
expectType<JSX.Element>(<Foo foo={1} ref="ref" />)
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<Foo />)
|
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(<Foo foo="bar" />)
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 07:08:17 +08:00
|
|
|
expectError(<Foo baz="bar" />)
|
2020-04-04 00:05:52 +08:00
|
|
|
|
|
|
|
// Explicit signature with props + emits
|
|
|
|
const Bar: FunctionalComponent<
|
|
|
|
{ foo: number },
|
|
|
|
{ update: (value: number) => void }
|
|
|
|
> = (props, { emit }) => {
|
|
|
|
expectType<number>(props.foo)
|
|
|
|
|
|
|
|
emit('update', 123)
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(emit('nope'))
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(emit('update'))
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(emit('update', 'nope'))
|
|
|
|
}
|
|
|
|
|
|
|
|
// assigning runtime options
|
|
|
|
Bar.props = {
|
|
|
|
foo: Number
|
|
|
|
}
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError((Bar.props = { foo: String }))
|
|
|
|
|
|
|
|
Bar.emits = {
|
|
|
|
update: value => value > 1
|
|
|
|
}
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError((Bar.emits = { baz: () => void 0 }))
|
|
|
|
|
|
|
|
// TSX
|
|
|
|
expectType<JSX.Element>(<Bar foo={1} />)
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
|
|
|
expectError(<Foo />)
|
|
|
|
// @ts-expect-error
|
2020-04-04 00:05:52 +08:00
|
|
|
expectError(<Bar foo="bar" />)
|
2020-06-09 22:17:42 +08:00
|
|
|
// @ts-expect-error
|
2020-04-04 07:08:17 +08:00
|
|
|
expectError(<Foo baz="bar" />)
|
2020-08-19 22:00:48 +08:00
|
|
|
|
|
|
|
const Baz: FunctionalComponent<{}, string[]> = (props, { emit }) => {
|
|
|
|
expectType<{}>(props)
|
|
|
|
expectType<(event: string) => void>(emit)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectType<Component>(Baz)
|
2020-09-22 22:05:37 +08:00
|
|
|
|
|
|
|
const Qux: FunctionalComponent<{}, ['foo', 'bar']> = (props, { emit }) => {
|
|
|
|
emit('foo')
|
|
|
|
emit('foo', 1, 2)
|
|
|
|
emit('bar')
|
|
|
|
emit('bar', 1, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectType<Component>(Qux)
|