Skip to content

Commit

Permalink
fix(types): accept primatives and falsy values in createElement child…
Browse files Browse the repository at this point in the history
…ren (#9154)

fixes #8498
  • Loading branch information
KaelWD authored and yyx990803 committed Dec 10, 2018
1 parent 77d406b commit d780dd2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
22 changes: 20 additions & 2 deletions types/test/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ Vue.component('provide-function', {
})
})

Vue.component('component-with-slot', {
render (h): VNode {
return h('div', this.$slots.default)
}
})

Vue.component('component-with-scoped-slot', {
render (h) {
interface ScopedSlotProps {
Expand All @@ -301,6 +307,12 @@ Vue.component('component-with-scoped-slot', {
h('child', {
// Passing down all slots from parent
scopedSlots: this.$scopedSlots
}),
h('child', {
// Passing down single slot from parent
scopedSlots: {
default: this.$scopedSlots.default
}
})
])
},
Expand All @@ -320,16 +332,22 @@ Vue.component('narrow-array-of-vnode-type', {
render (h): VNode {
const slot = this.$scopedSlots.default!({})
if (typeof slot === 'string') {
// <template slot-scope="data">bare string</template>
return h('span', slot)
} else if (Array.isArray(slot)) {
// template with multiple children
const first = slot[0]
if (!Array.isArray(first) && typeof first !== 'string') {
if (!Array.isArray(first) && typeof first !== 'string' && first) {
return first
} else {
return h()
}
} else {
} else if (slot) {
// <div slot-scope="data">bare VNode</div>
return slot
} else {
// empty template, slot === undefined
return h()
}
}
})
Expand Down
13 changes: 9 additions & 4 deletions types/vnode.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Vue } from "./vue";

export type ScopedSlot = (props: any) => VNodeChildrenArrayContents | VNode | string;
// Scoped slots can technically return anything if used from
// a render function, but this is "good enough" for templates
export type ScopedSlot = (props: any) => ScopedSlotChildren;
export type ScopedSlotChildren = ScopedSlotArrayContents | VNode | string | undefined;
export interface ScopedSlotArrayContents extends Array<ScopedSlotChildren> {}

export type VNodeChildren = VNodeChildrenArrayContents | [ScopedSlot] | string;
export interface VNodeChildrenArrayContents extends Array<VNode | string | VNodeChildrenArrayContents> {}
// Relaxed type compatible with $createElement
export type VNodeChildren = VNodeChildrenArrayContents | [ScopedSlot] | string | boolean | null | undefined;
export interface VNodeChildrenArrayContents extends Array<VNodeChildren | VNode> {}

export interface VNode {
tag?: string;
Expand All @@ -27,7 +32,7 @@ export interface VNodeComponentOptions {
Ctor: typeof Vue;
propsData?: object;
listeners?: object;
children?: VNodeChildren;
children?: VNode[];
tag?: string;
}

Expand Down

0 comments on commit d780dd2

Please sign in to comment.