types: improve typing
This commit is contained in:
parent
b68eb229c7
commit
8da5b007b1
@ -183,7 +183,7 @@ export function processExpression(
|
|||||||
// range is offset by -1 due to the wrapping parens when parsed
|
// range is offset by -1 due to the wrapping parens when parsed
|
||||||
const start = id.start - 1
|
const start = id.start - 1
|
||||||
const end = id.end - 1
|
const end = id.end - 1
|
||||||
const last = ids[i - 1] as any
|
const last = ids[i - 1]
|
||||||
const leadingText = rawExp.slice(last ? last.end - 1 : 0, start)
|
const leadingText = rawExp.slice(last ? last.end - 1 : 0, start)
|
||||||
if (leadingText.length || id.prefix) {
|
if (leadingText.length || id.prefix) {
|
||||||
children.push(leadingText + (id.prefix || ``))
|
children.push(leadingText + (id.prefix || ``))
|
||||||
|
@ -157,7 +157,9 @@ export function createAppAPI<HostNode, HostElement>(
|
|||||||
`It will be overwritten with the new value.`
|
`It will be overwritten with the new value.`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
context.provides[key as any] = value
|
// TypeScript doesn't allow symbols as index type
|
||||||
|
// https://github.com/Microsoft/TypeScript/issues/24587
|
||||||
|
context.provides[key as string] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,8 @@ export function provide<T>(key: InjectionKey<T> | string, value: T) {
|
|||||||
if (parentProvides === provides) {
|
if (parentProvides === provides) {
|
||||||
provides = currentInstance.provides = Object.create(parentProvides)
|
provides = currentInstance.provides = Object.create(parentProvides)
|
||||||
}
|
}
|
||||||
provides[key as any] = value
|
// TS doesn't allow symbol as index type
|
||||||
|
provides[key as string] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +31,8 @@ export function inject(key: InjectionKey<any> | string, defaultValue?: any) {
|
|||||||
if (currentInstance) {
|
if (currentInstance) {
|
||||||
const provides = currentInstance.provides
|
const provides = currentInstance.provides
|
||||||
if (key in provides) {
|
if (key in provides) {
|
||||||
return provides[key as any] as any
|
// TS doesn't allow symbol as index type
|
||||||
|
return provides[key as string]
|
||||||
} else if (defaultValue !== undefined) {
|
} else if (defaultValue !== undefined) {
|
||||||
return defaultValue
|
return defaultValue
|
||||||
} else if (__DEV__) {
|
} else if (__DEV__) {
|
||||||
|
@ -185,7 +185,7 @@ export function applyOptions(
|
|||||||
instance.renderContext === EMPTY_OBJ
|
instance.renderContext === EMPTY_OBJ
|
||||||
? (instance.renderContext = reactive({}))
|
? (instance.renderContext = reactive({}))
|
||||||
: instance.renderContext
|
: instance.renderContext
|
||||||
const ctx = instance.renderProxy as any
|
const ctx = instance.renderProxy!
|
||||||
const {
|
const {
|
||||||
// composition
|
// composition
|
||||||
mixins,
|
mixins,
|
||||||
@ -265,7 +265,7 @@ export function applyOptions(
|
|||||||
if (isString(raw)) {
|
if (isString(raw)) {
|
||||||
const handler = renderContext[raw]
|
const handler = renderContext[raw]
|
||||||
if (isFunction(handler)) {
|
if (isFunction(handler)) {
|
||||||
watch(getter, handler as any)
|
watch(getter, handler as WatchHandler)
|
||||||
} else if (__DEV__) {
|
} else if (__DEV__) {
|
||||||
// TODO warn invalid watch handler path
|
// TODO warn invalid watch handler path
|
||||||
}
|
}
|
||||||
|
@ -210,7 +210,7 @@ export function instanceWatch(
|
|||||||
cb: Function,
|
cb: Function,
|
||||||
options?: WatchOptions
|
options?: WatchOptions
|
||||||
): () => void {
|
): () => void {
|
||||||
const ctx = this.renderProxy as any
|
const ctx = this.renderProxy!
|
||||||
const getter = isString(source) ? () => ctx[source] : source.bind(ctx)
|
const getter = isString(source) ? () => ctx[source] : source.bind(ctx)
|
||||||
const stop = watch(getter, cb.bind(ctx), options)
|
const stop = watch(getter, cb.bind(ctx), options)
|
||||||
onBeforeUnmount(stop, this)
|
onBeforeUnmount(stop, this)
|
||||||
|
@ -132,8 +132,8 @@ export function createComponentInstance(
|
|||||||
type: vnode.type as Component,
|
type: vnode.type as Component,
|
||||||
root: null as any, // set later so it can point to itself
|
root: null as any, // set later so it can point to itself
|
||||||
next: null,
|
next: null,
|
||||||
subTree: null as any,
|
subTree: null as any, // will be set synchronously right after creation
|
||||||
update: null as any,
|
update: null as any, // will be set synchronously right after creation
|
||||||
render: null,
|
render: null,
|
||||||
renderProxy: null,
|
renderProxy: null,
|
||||||
propsProxy: null,
|
propsProxy: null,
|
||||||
|
@ -15,6 +15,7 @@ export type ComponentPublicInstance<
|
|||||||
M = {},
|
M = {},
|
||||||
PublicProps = P
|
PublicProps = P
|
||||||
> = {
|
> = {
|
||||||
|
[key: string]: any
|
||||||
$data: D
|
$data: D
|
||||||
$props: PublicProps
|
$props: PublicProps
|
||||||
$attrs: Data
|
$attrs: Data
|
||||||
|
@ -40,7 +40,7 @@ export function renderComponentRoot(
|
|||||||
slots,
|
slots,
|
||||||
emit
|
emit
|
||||||
})
|
})
|
||||||
: render(props, null as any)
|
: render(props, null as any /* we know it doesn't it */)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -11,11 +11,16 @@ import { ShapeFlags } from './shapeFlags'
|
|||||||
import { warn } from './warning'
|
import { warn } from './warning'
|
||||||
|
|
||||||
export type Slot = (...args: any[]) => VNodeChildren
|
export type Slot = (...args: any[]) => VNodeChildren
|
||||||
export type Slots = Readonly<{
|
|
||||||
|
export type InternalSlots = {
|
||||||
[name: string]: Slot
|
[name: string]: Slot
|
||||||
}>
|
}
|
||||||
|
|
||||||
|
export type Slots = Readonly<InternalSlots>
|
||||||
|
|
||||||
export type RawSlots = {
|
export type RawSlots = {
|
||||||
[name: string]: unknown
|
[name: string]: unknown
|
||||||
|
_compiled?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalizeSlotValue = (value: unknown): VNode[] =>
|
const normalizeSlotValue = (value: unknown): VNode[] =>
|
||||||
@ -40,17 +45,18 @@ export function resolveSlots(
|
|||||||
instance: ComponentInternalInstance,
|
instance: ComponentInternalInstance,
|
||||||
children: NormalizedChildren
|
children: NormalizedChildren
|
||||||
) {
|
) {
|
||||||
let slots: Slots | void
|
let slots: InternalSlots | void
|
||||||
if (instance.vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
|
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
|
// pre-normalized slots object generated by compiler
|
||||||
slots = children as Slots
|
slots = children as Slots
|
||||||
} else {
|
} else {
|
||||||
slots = {}
|
slots = {}
|
||||||
for (const key in children as RawSlots) {
|
for (const key in rawSlots) {
|
||||||
let value = (children as RawSlots)[key]
|
const value = rawSlots[key]
|
||||||
if (isFunction(value)) {
|
if (isFunction(value)) {
|
||||||
;(slots as any)[key] = normalizeSlot(key, value)
|
slots[key] = normalizeSlot(key, value)
|
||||||
} else if (value != null) {
|
} else if (value != null) {
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
warn(
|
warn(
|
||||||
@ -58,8 +64,8 @@ export function resolveSlots(
|
|||||||
`Prefer function slots for better performance.`
|
`Prefer function slots for better performance.`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
value = normalizeSlotValue(value)
|
const normalized = normalizeSlotValue(value)
|
||||||
;(slots as any)[key] = () => value
|
slots[key] = () => normalized
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1188,7 +1188,7 @@ export function createRenderer<
|
|||||||
nextVNode.component = instance
|
nextVNode.component = instance
|
||||||
instance.vnode = nextVNode
|
instance.vnode = nextVNode
|
||||||
instance.next = null
|
instance.next = null
|
||||||
resolveProps(instance, nextVNode.props, (nextVNode.type as any).props)
|
resolveProps(instance, nextVNode.props, (nextVNode.type as Component).props)
|
||||||
resolveSlots(instance, nextVNode.children)
|
resolveSlots(instance, nextVNode.children)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +104,8 @@ export function applyDirectives(vnode: VNode, directives: DirectiveArguments) {
|
|||||||
vnode = cloneVNode(vnode)
|
vnode = cloneVNode(vnode)
|
||||||
vnode.props = vnode.props != null ? extend({}, vnode.props) : {}
|
vnode.props = vnode.props != null ? extend({}, vnode.props) : {}
|
||||||
for (let i = 0; i < directives.length; i++) {
|
for (let i = 0; i < directives.length; i++) {
|
||||||
;(applyDirective as any)(vnode.props, instance, ...directives[i])
|
const [dir, value, arg, modifiers] = directives[i]
|
||||||
|
applyDirective(vnode.props, instance, dir, value, arg, modifiers)
|
||||||
}
|
}
|
||||||
} else if (__DEV__) {
|
} else if (__DEV__) {
|
||||||
warn(`applyDirectives can only be used inside render functions.`)
|
warn(`applyDirectives can only be used inside render functions.`)
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import { VNode, normalizeVNode } from './vnode'
|
import { VNode, normalizeVNode, VNodeChild } from './vnode'
|
||||||
import { ShapeFlags } from '.'
|
import { ShapeFlags } from '.'
|
||||||
import { isFunction } from '@vue/shared'
|
import { isFunction } from '@vue/shared'
|
||||||
import { ComponentInternalInstance } from './component'
|
import { ComponentInternalInstance } from './component'
|
||||||
|
import { Slots } from './componentSlots'
|
||||||
|
|
||||||
export const SuspenseSymbol = __DEV__ ? Symbol('Suspense key') : Symbol()
|
export const SuspenseSymbol = __DEV__ ? Symbol('Suspense key') : Symbol()
|
||||||
|
|
||||||
@ -62,14 +63,14 @@ export function normalizeSuspenseChildren(
|
|||||||
} {
|
} {
|
||||||
const { shapeFlag, children } = vnode
|
const { shapeFlag, children } = vnode
|
||||||
if (shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
|
if (shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
|
||||||
const { default: d, fallback } = children as any
|
const { default: d, fallback } = children as Slots
|
||||||
return {
|
return {
|
||||||
content: normalizeVNode(isFunction(d) ? d() : d),
|
content: normalizeVNode(isFunction(d) ? d() : d),
|
||||||
fallback: normalizeVNode(isFunction(fallback) ? fallback() : fallback)
|
fallback: normalizeVNode(isFunction(fallback) ? fallback() : fallback)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
content: normalizeVNode(children as any),
|
content: normalizeVNode(children as VNodeChild),
|
||||||
fallback: normalizeVNode(null)
|
fallback: normalizeVNode(null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,9 @@ import {
|
|||||||
} from '@vue/runtime-core'
|
} from '@vue/runtime-core'
|
||||||
import { ErrorCodes } from 'packages/runtime-core/src/errorHandling'
|
import { ErrorCodes } from 'packages/runtime-core/src/errorHandling'
|
||||||
|
|
||||||
interface Invoker extends Function {
|
interface Invoker extends EventListener {
|
||||||
value: EventValue
|
value: EventValue
|
||||||
lastUpdated?: number
|
lastUpdated: number
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventValue = (Function | Function[]) & {
|
type EventValue = (Function | Function[]) & {
|
||||||
@ -58,7 +58,7 @@ export function patchEvent(
|
|||||||
el.addEventListener(name, createInvoker(nextValue, instance))
|
el.addEventListener(name, createInvoker(nextValue, instance))
|
||||||
}
|
}
|
||||||
} else if (invoker) {
|
} else if (invoker) {
|
||||||
el.removeEventListener(name, invoker as any)
|
el.removeEventListener(name, invoker)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ function createInvoker(
|
|||||||
initialValue: any,
|
initialValue: any,
|
||||||
instance: ComponentInternalInstance | null
|
instance: ComponentInternalInstance | null
|
||||||
) {
|
) {
|
||||||
const invoker = ((e: Event) => {
|
const invoker: Invoker = (e: Event) => {
|
||||||
// async edge case #6566: inner click event triggers patch, event handler
|
// async edge case #6566: inner click event triggers patch, event handler
|
||||||
// attached to outer element during patch, and triggered again. This
|
// attached to outer element during patch, and triggered again. This
|
||||||
// happens because browsers fire microtask ticks between event propagation.
|
// happens because browsers fire microtask ticks between event propagation.
|
||||||
@ -94,7 +94,7 @@ function createInvoker(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}) as any
|
}
|
||||||
invoker.value = initialValue
|
invoker.value = initialValue
|
||||||
initialValue.invoker = invoker
|
initialValue.invoker = invoker
|
||||||
invoker.lastUpdated = getNow()
|
invoker.lastUpdated = getNow()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user