Skip to content

Commit

Permalink
fix(types): handling PropType<Function> with default value (#1896)
Browse files Browse the repository at this point in the history
fix #1891
  • Loading branch information
pikax authored Aug 19, 2020
1 parent 02dcc68 commit c2913d5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type DefaultFactory<T> = (props: Data) => T | null | undefined
interface PropOptions<T = any, D = T> {
type?: PropType<T> | true | null
required?: boolean
default?: D | DefaultFactory<D> | null | undefined
default?: D | DefaultFactory<D> | null | undefined | object
validator?(value: unknown): boolean
}

Expand Down
7 changes: 7 additions & 0 deletions test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('with object props', () => {
fff: (a: number, b: string) => { a: boolean }
hhh: boolean
ggg: 'foo' | 'bar'
ffff: (a: number, b: string) => { a: boolean }
validated?: string
}

Expand Down Expand Up @@ -90,6 +91,11 @@ describe('with object props', () => {
type: String as PropType<'foo' | 'bar'>,
default: 'foo'
},
// default + function
ffff: {
type: Function as PropType<(a: number, b: string) => { a: boolean }>,
default: (a: number, b: string) => ({ a: true })
},
validated: {
type: String,
// validator requires explicit annotation
Expand All @@ -113,6 +119,7 @@ describe('with object props', () => {
expectType<ExpectedProps['fff']>(props.fff)
expectType<ExpectedProps['hhh']>(props.hhh)
expectType<ExpectedProps['ggg']>(props.ggg)
expectType<ExpectedProps['ffff']>(props.ffff)
expectType<ExpectedProps['validated']>(props.validated)

// @ts-expect-error props should be readonly
Expand Down

0 comments on commit c2913d5

Please sign in to comment.