|
| 1 | +import type { FunctionalVaporComponent } from 'vue' |
| 2 | +import { expectType } from '../utils' |
| 3 | +import type { Block } from 'vue' |
| 4 | + |
| 5 | +// simple function signature |
| 6 | +const VaporComp = (props: { foo: number }) => [<div>123</div>] |
| 7 | + |
| 8 | +// TSX |
| 9 | +expectType<JSX.Element>(<VaporComp foo={1} />) |
| 10 | +expectType<JSX.Element>(<VaporComp foo={1} key="1" />) |
| 11 | +expectType<JSX.Element>(<VaporComp foo={1} ref="ref" />) |
| 12 | +// @ts-expect-error |
| 13 | +;<Foo /> |
| 14 | +// @ts-expect-error |
| 15 | +;<Foo foo="bar" /> |
| 16 | +// @ts-expect-error |
| 17 | +;<Foo baz="bar" /> |
| 18 | + |
| 19 | +// Explicit signature with props + emits |
| 20 | +const Bar: FunctionalVaporComponent< |
| 21 | + { foo: number }, |
| 22 | + { update: (value: number) => void } |
| 23 | +> = (props, { emit }) => { |
| 24 | + expectType<number>(props.foo) |
| 25 | + |
| 26 | + emit('update', 123) |
| 27 | + // @ts-expect-error |
| 28 | + emit('nope') |
| 29 | + // @ts-expect-error |
| 30 | + emit('update') |
| 31 | + // @ts-expect-error |
| 32 | + emit('update', 'nope') |
| 33 | + return [] |
| 34 | +} |
| 35 | + |
| 36 | +// assigning runtime options |
| 37 | +Bar.props = { |
| 38 | + foo: Number, |
| 39 | +} |
| 40 | +// @ts-expect-error |
| 41 | +Bar.props = { foo: String } |
| 42 | + |
| 43 | +Bar.emits = { |
| 44 | + update: value => value > 1, |
| 45 | +} |
| 46 | +// @ts-expect-error |
| 47 | +Bar.emits = { baz: () => void 0 } |
| 48 | + |
| 49 | +// TSX |
| 50 | +expectType<JSX.Element>(<Bar foo={1} onUpdate={() => {}} />) |
| 51 | +// @ts-expect-error |
| 52 | +;<Foo /> |
| 53 | +// @ts-expect-error |
| 54 | +;<Bar foo="bar" /> |
| 55 | +// @ts-expect-error |
| 56 | +;<Foo baz="bar" /> |
| 57 | + |
| 58 | +const Quux: FunctionalVaporComponent< |
| 59 | + {}, |
| 60 | + {}, |
| 61 | + { |
| 62 | + default: (props: { foo: number }) => Block |
| 63 | + optional?: (props: { foo: number }) => Block |
| 64 | + } |
| 65 | +> = (props, { emit, slots }) => { |
| 66 | + expectType<{ |
| 67 | + default: (scope: { foo: number }) => Block |
| 68 | + optional?: (scope: { foo: number }) => Block |
| 69 | + }>(slots) |
| 70 | + |
| 71 | + slots.default({ foo: 123 }) |
| 72 | + // @ts-expect-error |
| 73 | + slots.default({ foo: 'fesf' }) |
| 74 | + |
| 75 | + slots.optional?.({ foo: 123 }) |
| 76 | + // @ts-expect-error |
| 77 | + slots.optional?.({ foo: 'fesf' }) |
| 78 | + // @ts-expect-error |
| 79 | + slots.optional({ foo: 123 }) |
| 80 | + return [] |
| 81 | +} |
| 82 | +;<Quux /> |
0 commit comments