2020-11-25 04:12:59 +08:00
|
|
|
import {
|
|
|
|
expectType,
|
|
|
|
defineProps,
|
|
|
|
defineEmit,
|
2021-06-23 03:02:56 +08:00
|
|
|
defineEmits,
|
2020-11-25 04:12:59 +08:00
|
|
|
useContext,
|
2021-07-02 19:51:09 +08:00
|
|
|
useAttrs,
|
|
|
|
useSlots,
|
2021-06-27 09:35:00 +08:00
|
|
|
withDefaults,
|
2020-11-25 04:12:59 +08:00
|
|
|
Slots,
|
|
|
|
describe
|
|
|
|
} from './index'
|
|
|
|
|
|
|
|
describe('defineProps w/ type declaration', () => {
|
|
|
|
// type declaration
|
|
|
|
const props = defineProps<{
|
|
|
|
foo: string
|
|
|
|
}>()
|
|
|
|
// explicitly declared type should be refined
|
|
|
|
expectType<string>(props.foo)
|
|
|
|
// @ts-expect-error
|
|
|
|
props.bar
|
|
|
|
})
|
|
|
|
|
2021-06-27 09:11:57 +08:00
|
|
|
describe('defineProps w/ type declaration + withDefaults', () => {
|
|
|
|
const res = withDefaults(
|
|
|
|
defineProps<{
|
|
|
|
number?: number
|
|
|
|
arr?: string[]
|
|
|
|
obj?: { x: number }
|
|
|
|
fn?: (e: string) => void
|
|
|
|
x?: string
|
|
|
|
}>(),
|
2021-06-26 07:31:47 +08:00
|
|
|
{
|
2021-06-27 09:11:57 +08:00
|
|
|
number: 123,
|
|
|
|
arr: () => [],
|
2021-06-26 07:31:47 +08:00
|
|
|
obj: () => ({ x: 123 }),
|
2021-06-27 09:11:57 +08:00
|
|
|
fn: () => {}
|
2021-06-26 07:31:47 +08:00
|
|
|
}
|
|
|
|
)
|
2021-06-27 09:11:57 +08:00
|
|
|
|
|
|
|
res.number + 1
|
|
|
|
res.arr.push('hi')
|
|
|
|
res.obj.x
|
|
|
|
res.fn('hi')
|
|
|
|
// @ts-expect-error
|
|
|
|
res.x.slice()
|
2021-06-26 07:31:47 +08:00
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
describe('defineProps w/ runtime declaration', () => {
|
|
|
|
// runtime declaration
|
|
|
|
const props = defineProps({
|
|
|
|
foo: String,
|
|
|
|
bar: {
|
|
|
|
type: Number,
|
|
|
|
default: 1
|
|
|
|
},
|
|
|
|
baz: {
|
|
|
|
type: Array,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
expectType<{
|
|
|
|
foo?: string
|
|
|
|
bar: number
|
|
|
|
baz: unknown[]
|
|
|
|
}>(props)
|
|
|
|
|
|
|
|
props.foo && props.foo + 'bar'
|
|
|
|
props.bar + 1
|
|
|
|
// @ts-expect-error should be readonly
|
|
|
|
props.bar++
|
|
|
|
props.baz.push(1)
|
|
|
|
|
|
|
|
const props2 = defineProps(['foo', 'bar'])
|
|
|
|
props2.foo + props2.bar
|
|
|
|
// @ts-expect-error
|
|
|
|
props2.baz
|
|
|
|
})
|
|
|
|
|
2021-06-23 03:02:56 +08:00
|
|
|
describe('defineEmits w/ type declaration', () => {
|
|
|
|
const emit = defineEmits<(e: 'change') => void>()
|
2020-11-25 04:12:59 +08:00
|
|
|
emit('change')
|
|
|
|
// @ts-expect-error
|
|
|
|
emit()
|
|
|
|
// @ts-expect-error
|
|
|
|
emit('bar')
|
2021-03-30 04:38:07 +08:00
|
|
|
|
|
|
|
type Emits = { (e: 'foo' | 'bar'): void; (e: 'baz', id: number): void }
|
2021-06-23 03:02:56 +08:00
|
|
|
const emit2 = defineEmits<Emits>()
|
2021-03-30 04:38:07 +08:00
|
|
|
|
|
|
|
emit2('foo')
|
|
|
|
emit2('bar')
|
|
|
|
emit2('baz', 123)
|
|
|
|
// @ts-expect-error
|
|
|
|
emit2('baz')
|
2020-11-25 04:12:59 +08:00
|
|
|
})
|
|
|
|
|
2021-06-23 03:02:56 +08:00
|
|
|
describe('defineEmits w/ runtime declaration', () => {
|
|
|
|
const emit = defineEmits({
|
2020-11-25 04:12:59 +08:00
|
|
|
foo: () => {},
|
|
|
|
bar: null
|
|
|
|
})
|
|
|
|
emit('foo')
|
|
|
|
emit('bar', 123)
|
|
|
|
// @ts-expect-error
|
|
|
|
emit('baz')
|
|
|
|
|
2021-06-23 03:02:56 +08:00
|
|
|
const emit2 = defineEmits(['foo', 'bar'])
|
2020-11-25 04:12:59 +08:00
|
|
|
emit2('foo')
|
|
|
|
emit2('bar', 123)
|
|
|
|
// @ts-expect-error
|
|
|
|
emit2('baz')
|
|
|
|
})
|
|
|
|
|
2021-06-23 03:02:56 +08:00
|
|
|
describe('deprecated defineEmit', () => {
|
|
|
|
const emit = defineEmit({
|
|
|
|
foo: () => {},
|
|
|
|
bar: null
|
|
|
|
})
|
|
|
|
emit('foo')
|
|
|
|
emit('bar', 123)
|
|
|
|
// @ts-expect-error
|
|
|
|
emit('baz')
|
|
|
|
})
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
describe('useContext', () => {
|
|
|
|
const { attrs, emit, slots } = useContext()
|
|
|
|
expectType<Record<string, unknown>>(attrs)
|
|
|
|
expectType<(...args: any[]) => void>(emit)
|
|
|
|
expectType<Slots>(slots)
|
|
|
|
|
|
|
|
// @ts-expect-error
|
|
|
|
props.foo
|
|
|
|
// should be able to emit anything
|
|
|
|
emit('foo')
|
|
|
|
emit('bar')
|
|
|
|
})
|
2021-07-02 19:51:09 +08:00
|
|
|
|
|
|
|
describe('useAttrs', () => {
|
|
|
|
const attrs = useAttrs()
|
|
|
|
expectType<Record<string, unknown>>(attrs)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('useSlots', () => {
|
|
|
|
const slots = useSlots()
|
|
|
|
expectType<Slots>(slots)
|
|
|
|
})
|