Skip to content

Commit b9fb1b9

Browse files
committed
type
1 parent 4f59ad9 commit b9fb1b9

File tree

9 files changed

+55
-39
lines changed

9 files changed

+55
-39
lines changed

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import type {
1010
ComponentPublicInstance,
1111
ComponentOptions
1212
} from 'vue'
13-
13+
const IdentifySymbol = Symbol('vue-facing-decorator-identify')
14+
export interface BaseTypeIdentify {
15+
[IdentifySymbol]: undefined
16+
}
1417

1518
export const Base = class { } as {
16-
new(): ComponentPublicInstance
19+
new(): ComponentPublicInstance & BaseTypeIdentify
1720
}
1821

1922

src/option/emit.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { Cons, OptionBuilder } from '../component'
2-
import { obtainSlot } from '../utils'
3-
import type { WatchCallback } from 'vue'
2+
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
43
export type EmitConfig = null | string
5-
export function decorator(key?: string) {
4+
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, key?: string) {
5+
const slot = obtainSlot(proto)
6+
let map = slot.obtainMap<Map<string, EmitConfig>>('emit');
7+
map.set(name, typeof key === 'undefined' ? null : key)
8+
})
69

7-
return function (proto: any, name: string) {
8-
const slot = obtainSlot(proto)
9-
let map = slot.obtainMap<Map<string, EmitConfig>>('emit');
10-
map.set(name, typeof key === 'undefined' ? null : key)
11-
}
12-
}
1310

1411
export function build(cons: Cons, optionBuilder: OptionBuilder) {
1512
optionBuilder.methods ??= {}

src/option/inject.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11

22
import { Cons, OptionBuilder } from '../component'
3-
import { obtainSlot } from '../utils'
3+
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
44
export interface InjectConfig {
55
from?: string | Symbol
66
default?: any
77
}
88

9-
export function decorator(option?: InjectConfig) {
10-
return function (proto: any, name: string) {
11-
const slot = obtainSlot(proto)
12-
let map = slot.obtainMap<Map<string, InjectConfig>>('inject')
13-
const opt = Object.assign({}, option ?? {})
14-
map.set(name, opt)
15-
}
16-
}
9+
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, option?: InjectConfig) {
10+
const slot = obtainSlot(proto)
11+
let map = slot.obtainMap<Map<string, InjectConfig>>('inject')
12+
const opt = Object.assign({}, option ?? {})
13+
map.set(name, opt)
14+
})
15+
1716

1817
export function build(cons: Cons, optionBuilder: OptionBuilder) {
1918
optionBuilder.inject ??= {}

src/option/props.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
import { Cons, OptionBuilder } from '../component'
2-
import { obtainSlot } from '../utils'
2+
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
33
export interface PropsConfig {
44
type?: any
55
required?: boolean
66
default?: any
77
validator?(value: any): boolean;
88
}
99

10-
export function decorator(option?: PropsConfig) {
11-
12-
return function (proto: any, name: string) {
13-
const slot = obtainSlot(proto)
14-
let map = slot.obtainMap<Map<string, PropsConfig>>('props')
15-
const opt = Object.assign({}, option ?? {})
16-
map.set(name, opt)
17-
18-
}
19-
}
10+
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, option?: PropsConfig) {
11+
const slot = obtainSlot(proto)
12+
let map = slot.obtainMap<Map<string, PropsConfig>>('props')
13+
const opt = Object.assign({}, option ?? {})
14+
map.set(name, opt as PropsConfig)
15+
})
2016

2117
export function build(cons: Cons, optionBuilder: OptionBuilder) {
2218
optionBuilder.props ??= {}

src/option/ref.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Cons, OptionBuilder } from '../component'
2-
import { obtainSlot } from '../utils'
3-
export function decorator(proto: any, name: string) {
2+
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
3+
4+
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, option?: {}) {
45
const slot = obtainSlot(proto)
5-
let map=slot.obtainMap<Map<string, any>>('ref')
6+
let map = slot.obtainMap<Map<string, any>>('ref')
67
map.set(name, true)
7-
}
8+
})
9+
810

911
export function build(cons: Cons, optionBuilder: OptionBuilder) {
1012
optionBuilder.computed ??= {}

src/option/watch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Cons, OptionBuilder } from '../component'
2-
import { obtainSlot } from '../utils'
2+
import { obtainSlot, } from '../utils'
33
import type { WatchCallback } from 'vue'
44
export interface WatchConfig {
55
key: string
@@ -8,7 +8,8 @@ export interface WatchConfig {
88
deep?: boolean,
99
immediate?: boolean,
1010
}
11-
export function decorator(key: string, option?: Omit<WatchConfig, 'handler' | 'key'>) {
11+
type Option = Omit<WatchConfig, 'handler' | 'key'>
12+
export function decorator(key: string, option?: Option) {
1213

1314
return function (proto: any, name: string) {
1415
const slot = obtainSlot(proto)

src/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Base } from './index'
2+
import type { BaseTypeIdentify } from './index'
23
const SlotSymbol = Symbol('vue-facing-decorator-slot')
34
class Slot {
45
names: Map<string, Map<string, any>> = new Map
@@ -104,4 +105,21 @@ export function excludeNames(names: string[], slot: Slot) {
104105
export function getValidNames(obj: any, filter: (des: PropertyDescriptor, name: string) => boolean) {
105106
const descriptors = Object.getOwnPropertyDescriptors(obj)
106107
return Object.keys(descriptors).filter(name => filter(descriptors[name], name))
108+
}
109+
110+
export function optoinNullableMemberDecorator<T>(handler: { (proto: any, name: string, option?: T): any }) {
111+
function decorator(option?: T): any
112+
function decorator(proto: BaseTypeIdentify, name: any): any
113+
function decorator(optionOrProto?: T | BaseTypeIdentify, name?: any): any {
114+
if (name) {
115+
handler(optionOrProto, name)
116+
}
117+
else {
118+
return function (proto: any, name: any) {
119+
handler(proto, name, optionOrProto as T | undefined)
120+
}
121+
}
122+
}
123+
124+
return decorator
107125
}

test/option/emit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Component, Emit, Base } from '../../dist'
55

66
@Component
77
export class Comp extends Base {
8-
@Emit()
8+
@Emit
99
defaultEmit() {
1010
return 'defaultEmit value'
1111
}

test/option/props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function Full_validator() {
88
}
99
@Component
1010
export class Comp extends Base {
11-
@Prop()
11+
@Prop
1212
readonly propName!: any
1313

1414
@Prop({

0 commit comments

Comments
 (0)