types: improve typing

This commit is contained in:
Evan You
2019-10-08 12:43:13 -04:00
parent b68eb229c7
commit 8da5b007b1
13 changed files with 42 additions and 29 deletions

View File

@@ -11,11 +11,16 @@ import { ShapeFlags } from './shapeFlags'
import { warn } from './warning'
export type Slot = (...args: any[]) => VNodeChildren
export type Slots = Readonly<{
export type InternalSlots = {
[name: string]: Slot
}>
}
export type Slots = Readonly<InternalSlots>
export type RawSlots = {
[name: string]: unknown
_compiled?: boolean
}
const normalizeSlotValue = (value: unknown): VNode[] =>
@@ -40,17 +45,18 @@ export function resolveSlots(
instance: ComponentInternalInstance,
children: NormalizedChildren
) {
let slots: Slots | void
let slots: InternalSlots | void
if (instance.vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
if ((children as any)._compiled) {
const rawSlots = children as RawSlots
if (rawSlots._compiled) {
// pre-normalized slots object generated by compiler
slots = children as Slots
} else {
slots = {}
for (const key in children as RawSlots) {
let value = (children as RawSlots)[key]
for (const key in rawSlots) {
const value = rawSlots[key]
if (isFunction(value)) {
;(slots as any)[key] = normalizeSlot(key, value)
slots[key] = normalizeSlot(key, value)
} else if (value != null) {
if (__DEV__) {
warn(
@@ -58,8 +64,8 @@ export function resolveSlots(
`Prefer function slots for better performance.`
)
}
value = normalizeSlotValue(value)
;(slots as any)[key] = () => value
const normalized = normalizeSlotValue(value)
slots[key] = () => normalized
}
}
}