-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ts
53 lines (46 loc) · 1.19 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
let fieldStore = {}
/**
* 实体类基类
*/
class EntityBase<T> {
constructor(_props?: T) {}
}
/**
* 实体类装饰器, 自动构建类属性
*/
function Entity<T extends new (...args: any[]) => {}>(constructor: T) {
return class extends constructor {
// tslint:disable-next-line: no-any
constructor(...props: any[]) {
super()
if (!props.length) {
return
}
const prop = props[0]
Object.keys(this).map(item => this[item] && prop[item] && (this[item] = prop[item]))
Object.keys(fieldStore).map(item => (this[item] = prop[fieldStore[item]]))
fieldStore = {}
}
}
}
/**
* 字段别名装饰器, new class 优先级最高
*/
function FiledName(param?: string) {
if (!param) {
return
}
return (_target, key) => {
fieldStore[key] = param
}
}
@Entity
class Auth extends EntityBase<Auth> {
id: string
loginname: string
avatar_url: string
}
const auth = new Auth({ id: '', loginname: '', avatar_url: '' })
console.log('================auth====================')
console.log(auth)
console.log('====================================')