From 29311355efc7616e78f1bbe588e4ab754edf2ef8 Mon Sep 17 00:00:00 2001 From: cexbrayat Date: Mon, 18 May 2020 15:30:02 +0200 Subject: [PATCH] chore: typings maintenance in mount This commit tidies up the types in mount. The goal is to enable `strict: true` in the future on the project, and this commit fixes up a bunch of errors that the strict mode complains about. --- src/mount.ts | 70 +++++++++++++++++++++++++++++++--------------------- src/types.ts | 2 +- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/src/mount.ts b/src/mount.ts index f32817d4b..18d113223 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -12,12 +12,14 @@ import { ComponentOptionsWithArrayProps, ComponentOptionsWithoutProps, ExtractPropTypes, - Component + Component, + AppConfig, + VNodeProps } from 'vue' import { config } from './config' import { GlobalMountOptions } from './types' -import { mergeGlobalProperties, isString } from './utils' +import { mergeGlobalProperties } from './utils' import { processSlot } from './utils/compileSlots' import { createWrapper, VueWrapper } from './vue-wrapper' import { attachEmitListener } from './emitMixin' @@ -28,7 +30,6 @@ import { MOUNT_PARENT_NAME } from './constants' import { stubComponents } from './stubs' -import { parse } from '@vue/compiler-dom' type Slot = VNode | string | { render: Function } | Function @@ -89,17 +90,17 @@ export function mount( // normalise the incoming component const component = typeof originalComponent === 'function' - ? { + ? defineComponent({ setup: (_, { attrs, slots }) => () => h(originalComponent, attrs, slots) - } + }) : { ...originalComponent } const el = document.createElement('div') el.id = MOUNT_ELEMENT_ID if (options?.attachTo) { - let to: Element + let to: Element | null if (typeof options.attachTo === 'string') { to = document.querySelector(options.attachTo) if (!to) { @@ -117,29 +118,37 @@ export function mount( // handle any slots passed via mounting options const slots: VNodeNormalizedChildren = options?.slots && - Object.entries(options.slots).reduce((acc, [name, slot]) => { - // case of an SFC getting passed - if (typeof slot === 'object' && 'render' in slot) { - acc[name] = slot.render - return acc - } + Object.entries(options.slots).reduce( + ( + acc: { [key: string]: Function }, + [name, slot]: [string, Slot] + ): { [key: string]: Function } => { + // case of an SFC getting passed + if (typeof slot === 'object' && 'render' in slot) { + acc[name] = slot.render + return acc + } - if (typeof slot === 'function') { - acc[name] = slot - return acc - } + if (typeof slot === 'function') { + acc[name] = slot + return acc + } - if (typeof slot === 'object') { - acc[name] = () => slot - return acc - } + if (typeof slot === 'object') { + acc[name] = () => slot + return acc + } + + if (typeof slot === 'string') { + // slot is most probably a scoped slot string or a plain string + acc[name] = (props: VNodeProps) => h(processSlot(slot), props) + return acc + } - if (typeof slot === 'string') { - // slot is most probably a scoped slot string or a plain string - acc[name] = (props) => h(processSlot(slot), props) return acc - } - }, {}) + }, + {} + ) // override component data with mounting options data if (options?.data) { @@ -184,8 +193,10 @@ export function mount( if (global?.mocks) { const mixin = { beforeCreate() { - for (const [k, v] of Object.entries(global.mocks)) { - this[k] = v + for (const [k, v] of Object.entries( + global.mocks as { [key: string]: any } + )) { + ;(this as any)[k] = v } } } @@ -195,7 +206,10 @@ export function mount( // AppConfig if (global.config) { - for (const [k, v] of Object.entries(global.config)) { + for (const [k, v] of Object.entries(global.config) as [ + keyof Omit, + any + ][]) { app.config[k] = v } } diff --git a/src/types.ts b/src/types.ts index c0c3c83d2..7d9d3fafc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -21,7 +21,7 @@ export type FindAllComponentsSelector = NameSelector | string export type GlobalMountOptions = { plugins?: Plugin[] - config?: AppConfig + config?: Omit // isNativeTag is readonly, so we omit it mixins?: ComponentOptions[] mocks?: Record provide?: Record