Skip to content

Commit 652afc2

Browse files
committed
fix: add strong types to Slot.Map
1 parent 08bbc02 commit 652afc2

File tree

10 files changed

+41
-22
lines changed

10 files changed

+41
-22
lines changed

src/option/computed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { OptionBuilder } from '../optionBuilder'
66
export function build(cons: Cons, optionBuilder: OptionBuilder) {
77
optionBuilder.computed ??= {}
88
const slot = obtainSlot(cons.prototype)
9-
let map = slot.obtainMap<Map<string, any>>('computed')
10-
let vanillaMap = slot.obtainMap<Map<string, any>>('vanilla')
9+
let map = slot.obtainMap('computed')
10+
let vanillaMap = slot.obtainMap('vanilla')
1111
const protoArr = toComponentReverse(cons.prototype)
1212
protoArr.forEach(proto => {
1313
getValidNames(proto, (des, name) => {

src/option/emit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
44
export type EmitConfig = null | string
55
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, key?: string) {
66
const slot = obtainSlot(proto)
7-
let map = slot.obtainMap<Map<string, EmitConfig>>('emit');
7+
let map = slot.obtainMap('emit');
88
map.set(name, typeof key === 'undefined' ? null : key)
99
})
1010

src/option/inject.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface InjectConfig {
99

1010
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, option?: InjectConfig) {
1111
const slot = obtainSlot(proto)
12-
let map = slot.obtainMap<Map<string, InjectConfig>>('inject')
12+
let map = slot.obtainMap('inject')
1313
const opt = Object.assign({}, option ?? {})
1414
map.set(name, opt)
1515
})
@@ -18,7 +18,7 @@ export const decorator = optoinNullableMemberDecorator(function (proto: any, nam
1818
export function build(cons: Cons, optionBuilder: OptionBuilder) {
1919
optionBuilder.inject ??= {}
2020
const slot = obtainSlot(cons.prototype)
21-
const names = slot.obtainMap<Map<string, InjectConfig>>('inject')
21+
const names = slot.obtainMap('inject')
2222
if (names) {
2323
names.forEach((value, name) => {
2424
optionBuilder.inject![name] = value

src/option/methodsAndHooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const HookNames = [
2626
export type HookConfig = null
2727
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string) {
2828
const slot = obtainSlot(proto)
29-
let map = slot.obtainMap<Map<string, HookConfig>>('hooks');
29+
let map = slot.obtainMap('hooks');
3030
map.set(name, null)
3131
})
3232

src/option/props.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ export interface PropsConfig {
1010

1111
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, option?: PropsConfig) {
1212
const slot = obtainSlot(proto)
13-
let map = slot.obtainMap<Map<string, PropsConfig>>('props')
13+
let map = slot.obtainMap('props')
1414
const opt = Object.assign({}, option ?? {})
1515
map.set(name, opt as PropsConfig)
1616
})
1717

1818
export function build(cons: Cons, optionBuilder: OptionBuilder) {
1919
optionBuilder.props ??= {}
2020
const slot = obtainSlot(cons.prototype)
21-
const names = slot.obtainMap<Map<string, PropsConfig>>('props')
21+
const names = slot.obtainMap('props')
2222

2323
if (names) {
2424
names.forEach((value, name) => {

src/option/ref.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
44

55
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, option?: {}) {
66
const slot = obtainSlot(proto)
7-
let map = slot.obtainMap<Map<string, any>>('ref')
7+
let map = slot.obtainMap('ref')
88
map.set(name, true)
99
})
1010

1111

1212
export function build(cons: Cons, optionBuilder: OptionBuilder) {
1313
const slot = obtainSlot(cons.prototype)
14-
const names = slot.obtainMap<Map<string, any>>('ref')!
14+
const names = slot.obtainMap('ref')!
1515
if (names) {
1616
applyAccessors(optionBuilder, (ctx: any) => {
1717
const data: Map<string, { get: () => any, set: undefined }> = new Map

src/option/vanilla.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
22

33
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, option?: {}) {
44
const slot = obtainSlot(proto)
5-
let map = slot.obtainMap<Map<string, any>>('vanilla')
5+
let map = slot.obtainMap('vanilla')
66
map.set(name, true)
77
})

src/option/vmodel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ export const decorator = optoinNullableMemberDecorator(function (proto: any, nam
1515
delete propsConfig.name
1616
}
1717
PropsDecorator(propsConfig)(proto, vmodelName)
18-
let map = slot.obtainMap<Map<string, VModelConfig>>('v-model')
18+
let map = slot.obtainMap('v-model')
1919
map.set(name, option)
2020
})
2121

2222

2323
export function build(cons: Cons, optionBuilder: OptionBuilder) {
2424
optionBuilder.computed ??= {}
2525
const slot = obtainSlot(cons.prototype)
26-
const names = slot.obtainMap<Map<string, VModelConfig>>('v-model')!
26+
const names = slot.obtainMap('v-model')!
2727
if (names && names.size > 0) {
2828
names.forEach((value, name) => {
2929
let vmodelName = (value && value.name) ?? 'modelValue'

src/option/watch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Option = Omit<WatchConfig, 'handler' | 'key'>
1313
export function decorator(key: string, option?: Option) {
1414
return function (proto: any, name: string) {
1515
const slot = obtainSlot(proto)
16-
let map = slot.obtainMap<Map<string, WatchConfig | WatchConfig[]>>('watch');
16+
let map = slot.obtainMap('watch');
1717
const opt = Object.assign({}, option ?? {}, {
1818
key: key,
1919
handler: proto[name]
@@ -35,7 +35,7 @@ export function decorator(key: string, option?: Option) {
3535
export function build(cons: Cons, optionBuilder: OptionBuilder) {
3636
optionBuilder.watch ??= {}
3737
const slot = obtainSlot(cons.prototype)
38-
const names = slot.obtainMap<Map<string, WatchConfig | WatchConfig[]>>('watch')
38+
const names = slot.obtainMap('watch')
3939
if (names) {
4040
names.forEach((value, name) => {
4141
const values = Array.isArray(value) ? value : [value]

src/utils.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
import { Base } from './index'
22
import type { BaseTypeIdentify } from './index'
3+
import type { InjectConfig } from "./option/inject";
4+
import type { EmitConfig } from "./option/emit";
5+
import type { PropsConfig } from "./option/props";
6+
import type { HookConfig } from "./option/methodsAndHooks";
7+
import type { VModelConfig } from "./option/vmodel";
8+
import type { WatchConfig } from "./option/watch";
9+
310
const SlotSymbol = Symbol('vue-facing-decorator-slot')
11+
12+
export type SlotMapTypes = {
13+
vanilla: Map<string, boolean>
14+
computed: Map<string, boolean>
15+
inject: Map<string, InjectConfig>
16+
emit: Map<string, EmitConfig>
17+
emits: Map<string, boolean>
18+
props: Map<string, PropsConfig>
19+
hooks: Map<string, HookConfig>
20+
'v-model': Map<string, VModelConfig>
21+
watch: Map<string, WatchConfig | WatchConfig[]>
22+
ref: Map<string, boolean>
23+
}
24+
425
class Slot {
526
master: any
627
constructor(master: any) {
728
this.master = master
829
}
9-
names: Map<string, Map<string, any>> = new Map
10-
obtainMap<T extends Map<string, any>>(name: string): T {
30+
names: Map<string, SlotMapTypes[keyof SlotMapTypes]> = new Map()
31+
obtainMap<T extends keyof SlotMapTypes>(name: T): SlotMapTypes[T] {
1132
let map = this.names.get(name)
1233
if (!map) {
13-
14-
map = new Map
34+
map = new Map()
1535
this.names.set(name, map)
16-
} else {
17-
1836
}
19-
return map as any
37+
return map as SlotMapTypes[T]
2038
}
2139
inComponent = false
2240
cachedVueComponent: any = null
@@ -33,6 +51,7 @@ export function makeSlot(obj: any): Slot {
3351
})
3452
return slot
3553
}
54+
3655
export function getSlot(obj: any): Slot | undefined {
3756

3857
return Object.getOwnPropertyDescriptor(obj, SlotSymbol)?.value

0 commit comments

Comments
 (0)