소스 검색

types: type sync for 2.6 features

Evan You 7 년 전
부모
커밋
0cf6aaddb5
3개의 변경된 파일13개의 추가작업 그리고 7개의 파일을 삭제
  1. 6 2
      src/core/vdom/helpers/normalize-scoped-slots.js
  2. 5 1
      types/test/options-test.ts
  3. 2 4
      types/vnode.d.ts

+ 6 - 2
src/core/vdom/helpers/normalize-scoped-slots.js

@@ -1,5 +1,7 @@
 /* @flow */
 
+import { normalizeChildren } from 'core/vdom/helpers/normalize-children'
+
 export function normalizeScopedSlots (
   slots: { [key: string]: Function } | void,
   normalSlots: { [key: string]: Array<VNode> }
@@ -27,10 +29,12 @@ export function normalizeScopedSlots (
   return res
 }
 
-function normalizeScopedSlot(fn: Function) {
+function normalizeScopedSlot(fn: Function): Function {
   return scope => {
     const res = fn(scope)
-    return Array.isArray(res) ? res : res ? [res] : res
+    return res && typeof res === 'object'
+      ? [res] // single vnode
+      : normalizeChildren(res)
   }
 }
 

+ 5 - 1
types/test/options-test.ts

@@ -338,8 +338,12 @@ Vue.component('component-with-scoped-slot', {
   components: {
     child: {
       render (this: Vue, h: CreateElement) {
+        const defaultSlot = this.$scopedSlots['default']!({ msg: 'hi' })
+        defaultSlot && defaultSlot.forEach(vnode => {
+          vnode.tag
+        })
         return h('div', [
-          this.$scopedSlots['default']!({ msg: 'hi' }),
+          defaultSlot,
           this.$scopedSlots['item']!({ msg: 'hello' })
         ])
       }

+ 2 - 4
types/vnode.d.ts

@@ -1,10 +1,8 @@
 import { Vue } from "./vue";
 
-// Scoped slots can technically return anything if used from
-// a render function, but this is "good enough" for templates
+// Scoped slots are guaranteed to return Array of VNodes starting in 2.6
 export type ScopedSlot = (props: any) => ScopedSlotChildren;
-export type ScopedSlotChildren = ScopedSlotArrayContents | VNode | string | undefined;
-export interface ScopedSlotArrayContents extends Array<ScopedSlotChildren> {}
+export type ScopedSlotChildren = VNode[] | undefined;
 
 // Relaxed type compatible with $createElement
 export type VNodeChildren = VNodeChildrenArrayContents | [ScopedSlot] | string | boolean | null | undefined;