Skip to content

Commit 112f29a

Browse files
authored
Merge pull request #123 from cexbrayat/chore/mount-ts
chore: typings maintenance in mount
2 parents 3630dd7 + 2931135 commit 112f29a

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

src/mount.ts

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import {
1212
ComponentOptionsWithArrayProps,
1313
ComponentOptionsWithoutProps,
1414
ExtractPropTypes,
15-
Component
15+
Component,
16+
AppConfig,
17+
VNodeProps
1618
} from 'vue'
1719

1820
import { config } from './config'
1921
import { GlobalMountOptions } from './types'
20-
import { mergeGlobalProperties, isString } from './utils'
22+
import { mergeGlobalProperties } from './utils'
2123
import { processSlot } from './utils/compileSlots'
2224
import { createWrapper, VueWrapper } from './vue-wrapper'
2325
import { attachEmitListener } from './emitMixin'
@@ -28,7 +30,6 @@ import {
2830
MOUNT_PARENT_NAME
2931
} from './constants'
3032
import { stubComponents } from './stubs'
31-
import { parse } from '@vue/compiler-dom'
3233

3334
type Slot = VNode | string | { render: Function } | Function
3435

@@ -89,17 +90,17 @@ export function mount(
8990
// normalise the incoming component
9091
const component =
9192
typeof originalComponent === 'function'
92-
? {
93+
? defineComponent({
9394
setup: (_, { attrs, slots }) => () =>
9495
h(originalComponent, attrs, slots)
95-
}
96+
})
9697
: { ...originalComponent }
9798

9899
const el = document.createElement('div')
99100
el.id = MOUNT_ELEMENT_ID
100101

101102
if (options?.attachTo) {
102-
let to: Element
103+
let to: Element | null
103104
if (typeof options.attachTo === 'string') {
104105
to = document.querySelector(options.attachTo)
105106
if (!to) {
@@ -117,29 +118,37 @@ export function mount(
117118
// handle any slots passed via mounting options
118119
const slots: VNodeNormalizedChildren =
119120
options?.slots &&
120-
Object.entries(options.slots).reduce((acc, [name, slot]) => {
121-
// case of an SFC getting passed
122-
if (typeof slot === 'object' && 'render' in slot) {
123-
acc[name] = slot.render
124-
return acc
125-
}
121+
Object.entries(options.slots).reduce(
122+
(
123+
acc: { [key: string]: Function },
124+
[name, slot]: [string, Slot]
125+
): { [key: string]: Function } => {
126+
// case of an SFC getting passed
127+
if (typeof slot === 'object' && 'render' in slot) {
128+
acc[name] = slot.render
129+
return acc
130+
}
126131

127-
if (typeof slot === 'function') {
128-
acc[name] = slot
129-
return acc
130-
}
132+
if (typeof slot === 'function') {
133+
acc[name] = slot
134+
return acc
135+
}
131136

132-
if (typeof slot === 'object') {
133-
acc[name] = () => slot
134-
return acc
135-
}
137+
if (typeof slot === 'object') {
138+
acc[name] = () => slot
139+
return acc
140+
}
141+
142+
if (typeof slot === 'string') {
143+
// slot is most probably a scoped slot string or a plain string
144+
acc[name] = (props: VNodeProps) => h(processSlot(slot), props)
145+
return acc
146+
}
136147

137-
if (typeof slot === 'string') {
138-
// slot is most probably a scoped slot string or a plain string
139-
acc[name] = (props) => h(processSlot(slot), props)
140148
return acc
141-
}
142-
}, {})
149+
},
150+
{}
151+
)
143152

144153
// override component data with mounting options data
145154
if (options?.data) {
@@ -184,8 +193,10 @@ export function mount(
184193
if (global?.mocks) {
185194
const mixin = {
186195
beforeCreate() {
187-
for (const [k, v] of Object.entries(global.mocks)) {
188-
this[k] = v
196+
for (const [k, v] of Object.entries(
197+
global.mocks as { [key: string]: any }
198+
)) {
199+
;(this as any)[k] = v
189200
}
190201
}
191202
}
@@ -195,7 +206,10 @@ export function mount(
195206

196207
// AppConfig
197208
if (global.config) {
198-
for (const [k, v] of Object.entries(global.config)) {
209+
for (const [k, v] of Object.entries(global.config) as [
210+
keyof Omit<AppConfig, 'isNativeTag'>,
211+
any
212+
][]) {
199213
app.config[k] = v
200214
}
201215
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type FindAllComponentsSelector = NameSelector | string
2121

2222
export type GlobalMountOptions = {
2323
plugins?: Plugin[]
24-
config?: AppConfig
24+
config?: Omit<AppConfig, 'isNativeTag'> // isNativeTag is readonly, so we omit it
2525
mixins?: ComponentOptions[]
2626
mocks?: Record<string, any>
2727
provide?: Record<any, any>

0 commit comments

Comments
 (0)