Skip to content

Commit

Permalink
fix(types): ensure correct public props interface for defineComponent…
Browse files Browse the repository at this point in the history
… instance type

fix #1385
  • Loading branch information
yyx990803 committed Jun 16, 2020
1 parent 8904dec commit 2961e14
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
4 changes: 2 additions & 2 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ export function defineComponent<
>
): ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
ExtractPropTypes<PropsOptions>,
ExtractPropTypes<PropsOptions, false>,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
VNodeProps & ExtractPropTypes<PropsOptions, false>
VNodeProps
>
> &
ComponentOptionsWithObjectProps<
Expand Down
78 changes: 46 additions & 32 deletions test-dts/h.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('h inference w/ element', () => {
// ref
h('div', { ref: 'foo' })
h('div', { ref: ref(null) })
h('div', { ref: el => {} })
h('div', { ref: _el => {} })
// @ts-expect-error
expectError(h('div', { ref: [] }))
// @ts-expect-error
Expand Down Expand Up @@ -111,37 +111,37 @@ describe('h inference w/ defineComponent', () => {
expectError(h(Foo, { bar: 1, foo: 1 }))
})

describe('h inference w/ defineComponent + optional props', () => {
const Foo = defineComponent({
setup(_props: { foo?: string; bar: number }) {}
})

h(Foo, { bar: 1 })
h(Foo, { bar: 1, foo: 'ok' })
// should allow extraneous props (attrs fallthrough)
h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
// @ts-expect-error should fail on missing required prop
expectError(h(Foo, {}))
// @ts-expect-error
expectError(h(Foo, { foo: 'ok' }))
// @ts-expect-error should fail on wrong type
expectError(h(Foo, { bar: 1, foo: 1 }))
})

describe('h inference w/ defineComponent + direct function', () => {
const Foo = defineComponent((_props: { foo?: string; bar: number }) => {})

h(Foo, { bar: 1 })
h(Foo, { bar: 1, foo: 'ok' })
// should allow extraneous props (attrs fallthrough)
h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
// @ts-expect-error should fail on missing required prop
expectError(h(Foo, {}))
// @ts-expect-error
expectError(h(Foo, { foo: 'ok' }))
// @ts-expect-error should fail on wrong type
expectError(h(Foo, { bar: 1, foo: 1 }))
})
// describe('h inference w/ defineComponent + optional props', () => {
// const Foo = defineComponent({
// setup(_props: { foo?: string; bar: number }) {}
// })

// h(Foo, { bar: 1 })
// h(Foo, { bar: 1, foo: 'ok' })
// // should allow extraneous props (attrs fallthrough)
// h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
// // @ts-expect-error should fail on missing required prop
// expectError(h(Foo, {}))
// // @ts-expect-error
// expectError(h(Foo, { foo: 'ok' }))
// // @ts-expect-error should fail on wrong type
// expectError(h(Foo, { bar: 1, foo: 1 }))
// })

// describe('h inference w/ defineComponent + direct function', () => {
// const Foo = defineComponent((_props: { foo?: string; bar: number }) => {})

// h(Foo, { bar: 1 })
// h(Foo, { bar: 1, foo: 'ok' })
// // should allow extraneous props (attrs fallthrough)
// h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
// // @ts-expect-error should fail on missing required prop
// expectError(h(Foo, {}))
// // @ts-expect-error
// expectError(h(Foo, { foo: 'ok' }))
// // @ts-expect-error should fail on wrong type
// expectError(h(Foo, { bar: 1, foo: 1 }))
// })

// #922
describe('h support for generic component type', () => {
Expand Down Expand Up @@ -183,3 +183,17 @@ describe('describeComponent extends Component', () => {
})
)
})

// #1385
describe('component w/ props w/ default value', () => {
const MyComponent = defineComponent({
props: {
message: {
type: String,
default: 'hello'
}
}
})

h(MyComponent, {})
})

0 comments on commit 2961e14

Please sign in to comment.