feat(sfc): withDefaults helper

This commit is contained in:
Evan You
2021-06-26 21:11:57 -04:00
parent 3ffc7be864
commit 4c5844a9ca
9 changed files with 492 additions and 165 deletions

View File

@@ -1,3 +1,4 @@
import { withDefaults } from '../packages/runtime-core/src/apiSetupHelpers'
import {
expectType,
defineProps,
@@ -19,30 +20,29 @@ describe('defineProps w/ type declaration', () => {
props.bar
})
describe('defineProps w/ type declaration + defaults', () => {
defineProps<{
number?: number
arr?: string[]
arr2?: string[]
obj?: { x: number }
obj2?: { x: number }
obj3?: { x: number }
}>(
{},
describe('defineProps w/ type declaration + withDefaults', () => {
const res = withDefaults(
defineProps<{
number?: number
arr?: string[]
obj?: { x: number }
fn?: (e: string) => void
x?: string
}>(),
{
number: 1,
arr: () => [''],
// @ts-expect-error not using factory
arr2: [''],
number: 123,
arr: () => [],
obj: () => ({ x: 123 }),
// @ts-expect-error not using factory
obj2: { x: 123 },
// @ts-expect-error factory return type does not match
obj3: () => ({ x: 'foo' })
fn: () => {}
}
)
res.number + 1
res.arr.push('hi')
res.obj.x
res.fn('hi')
// @ts-expect-error
res.x.slice()
})
describe('defineProps w/ runtime declaration', () => {