]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types: improve typing
authorEvan You <yyx990803@gmail.com>
Tue, 8 Oct 2019 16:43:13 +0000 (12:43 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 8 Oct 2019 16:43:13 +0000 (12:43 -0400)
13 files changed:
packages/compiler-core/src/transforms/transformExpression.ts
packages/runtime-core/src/apiApp.ts
packages/runtime-core/src/apiInject.ts
packages/runtime-core/src/apiOptions.ts
packages/runtime-core/src/apiWatch.ts
packages/runtime-core/src/component.ts
packages/runtime-core/src/componentProxy.ts
packages/runtime-core/src/componentRenderUtils.ts
packages/runtime-core/src/componentSlots.ts
packages/runtime-core/src/createRenderer.ts
packages/runtime-core/src/directives.ts
packages/runtime-core/src/suspense.ts
packages/runtime-dom/src/modules/events.ts

index 1c5d67dedcdbd5ea434ea46718cb4f03c962c454..fa3ef6b35ddb66a9b9e27ea5a6aabf1b5980dfcb 100644 (file)
@@ -183,7 +183,7 @@ export function processExpression(
     // range is offset by -1 due to the wrapping parens when parsed
     const start = id.start - 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)
     if (leadingText.length || id.prefix) {
       children.push(leadingText + (id.prefix || ``))
index 99ab5b78d72121feff9fd81be6d6aeee7a70802a..ecf26fabc4ef95aea5a8264cac45ae7654afb198 100644 (file)
@@ -157,7 +157,9 @@ export function createAppAPI<HostNode, HostElement>(
               `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
       }
     }
 
index 3766d9f1cdf11a5d467004951f06b1a30703421f..7eb076f1a7e23efd71e6fdae291e0c5d69c08b24 100644 (file)
@@ -20,7 +20,8 @@ export function provide<T>(key: InjectionKey<T> | string, value: T) {
     if (parentProvides === provides) {
       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) {
     const provides = currentInstance.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) {
       return defaultValue
     } else if (__DEV__) {
index 30b6521d4e8fdcf0d85daf84121567e94f882436..fd3f2ff8d0b0c9c702635317d0b98e74e26ae951 100644 (file)
@@ -185,7 +185,7 @@ export function applyOptions(
     instance.renderContext === EMPTY_OBJ
       ? (instance.renderContext = reactive({}))
       : instance.renderContext
-  const ctx = instance.renderProxy as any
+  const ctx = instance.renderProxy!
   const {
     // composition
     mixins,
@@ -265,7 +265,7 @@ export function applyOptions(
       if (isString(raw)) {
         const handler = renderContext[raw]
         if (isFunction(handler)) {
-          watch(getter, handler as any)
+          watch(getter, handler as WatchHandler)
         } else if (__DEV__) {
           // TODO warn invalid watch handler path
         }
index 5371a8f59a4e02c03aacc37239864d380587eb3f..8a72d686047566df891d6af8327bfdbfc159d744 100644 (file)
@@ -210,7 +210,7 @@ export function instanceWatch(
   cb: Function,
   options?: WatchOptions
 ): () => void {
-  const ctx = this.renderProxy as any
+  const ctx = this.renderProxy!
   const getter = isString(source) ? () => ctx[source] : source.bind(ctx)
   const stop = watch(getter, cb.bind(ctx), options)
   onBeforeUnmount(stop, this)
index 75a2876aa5593e0b9a5d69e66bde0f1bd05801c3..0cdbf04be281306be621142a86ea3c330fdeebb4 100644 (file)
@@ -132,8 +132,8 @@ export function createComponentInstance(
     type: vnode.type as Component,
     root: null as any, // set later so it can point to itself
     next: null,
-    subTree: null as any,
-    update: null as any,
+    subTree: null as any, // will be set synchronously right after creation
+    update: null as any, // will be set synchronously right after creation
     render: null,
     renderProxy: null,
     propsProxy: null,
index 49727b3593257556fcb61c575f03b9bbf82041d4..e02496ed36b421e45e84138271c496f3931e2ec6 100644 (file)
@@ -15,6 +15,7 @@ export type ComponentPublicInstance<
   M = {},
   PublicProps = P
 > = {
+  [key: string]: any
   $data: D
   $props: PublicProps
   $attrs: Data
index 554430c54d8579f0888676b720bf20321f39ff16..cdd232ee960692c3f0fe5d6991cdb1f32b4d8c07 100644 (file)
@@ -40,7 +40,7 @@ export function renderComponentRoot(
               slots,
               emit
             })
-          : render(props, null as any)
+          : render(props, null as any /* we know it doesn't it */)
       )
     }
   } catch (err) {
index 534caed5f2877b8d1f74e325e07d834249d7d5e0..8399bd352fc8955a4743a0a9b4c2bdbe33c311ba 100644 (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
         }
       }
     }
index 2d230ec712e9d56c5d478e9fcb9ddc3b2cd59914..3a90d1e0f416e5f71630bbfd079b2e516e8c4550 100644 (file)
@@ -1188,7 +1188,7 @@ export function createRenderer<
     nextVNode.component = instance
     instance.vnode = nextVNode
     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)
   }
 
index 09caf36bed54b9955f4d6492d6cbc3650ca681dc..e3f84dd7083a18fc1f1c80c0324a79472b3ac4b8 100644 (file)
@@ -104,7 +104,8 @@ export function applyDirectives(vnode: VNode, directives: DirectiveArguments) {
     vnode = cloneVNode(vnode)
     vnode.props = vnode.props != null ? extend({}, vnode.props) : {}
     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__) {
     warn(`applyDirectives can only be used inside render functions.`)
index 09a067ab017f068c1c0c8bab57cf8adda3a6102c..c852f424b9cef155b0d05296d7963b8a04d46809 100644 (file)
@@ -1,7 +1,8 @@
-import { VNode, normalizeVNode } from './vnode'
+import { VNode, normalizeVNode, VNodeChild } from './vnode'
 import { ShapeFlags } from '.'
 import { isFunction } from '@vue/shared'
 import { ComponentInternalInstance } from './component'
+import { Slots } from './componentSlots'
 
 export const SuspenseSymbol = __DEV__ ? Symbol('Suspense key') : Symbol()
 
@@ -62,14 +63,14 @@ export function normalizeSuspenseChildren(
 } {
   const { shapeFlag, children } = vnode
   if (shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
-    const { default: d, fallback } = children as any
+    const { default: d, fallback } = children as Slots
     return {
       content: normalizeVNode(isFunction(d) ? d() : d),
       fallback: normalizeVNode(isFunction(fallback) ? fallback() : fallback)
     }
   } else {
     return {
-      content: normalizeVNode(children as any),
+      content: normalizeVNode(children as VNodeChild),
       fallback: normalizeVNode(null)
     }
   }
index 7674f69b52a53cc8c468a30d49ce59b2b5968e79..7d572e4910a8bfcf56531835b29ca8b773fd01fe 100644 (file)
@@ -5,9 +5,9 @@ import {
 } from '@vue/runtime-core'
 import { ErrorCodes } from 'packages/runtime-core/src/errorHandling'
 
-interface Invoker extends Function {
+interface Invoker extends EventListener {
   value: EventValue
-  lastUpdated?: number
+  lastUpdated: number
 }
 
 type EventValue = (Function | Function[]) & {
@@ -58,7 +58,7 @@ export function patchEvent(
       el.addEventListener(name, createInvoker(nextValue, instance))
     }
   } else if (invoker) {
-    el.removeEventListener(name, invoker as any)
+    el.removeEventListener(name, invoker)
   }
 }
 
@@ -66,7 +66,7 @@ function createInvoker(
   initialValue: any,
   instance: ComponentInternalInstance | null
 ) {
-  const invoker = ((e: Event) => {
+  const invoker: Invoker = (e: Event) => {
     // async edge case #6566: inner click event triggers patch, event handler
     // attached to outer element during patch, and triggered again. This
     // happens because browsers fire microtask ticks between event propagation.
@@ -94,7 +94,7 @@ function createInvoker(
         )
       }
     }
-  }) as any
+  }
   invoker.value = initialValue
   initialValue.invoker = invoker
   invoker.lastUpdated = getNow()