-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.ts
40 lines (35 loc) · 961 Bytes
/
base.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
/**
* Reflect Metadata 基础使用
*
* @author wujohns
* @date 19/04/17
*/
import 'reflect-metadata'
@Reflect.metadata('inClass', 'AAA')
class Test {
@Reflect.metadata('inProperty', 'BBB')
private ele
@Reflect.metadata('inMethod', 'CCC')
public hello (): string {
return 'hello'
}
@Reflect.metadata('inAccessor', 'DDD')
public get elee () {
return 'elee'
}
}
// 获取相应的描述
const testObj = new Test()
console.log(Reflect.getMetadata('inClass', Test)) // 'AAA'
console.log(Reflect.getMetadata('inProperty', testObj, 'ele')) // 'BBB
console.log(Reflect.getMetadata('inMethod', testObj, 'hello')) // 'CCC'
console.log(Reflect.getMetadata('inAccessor', testObj, 'elee')) // 'DDD'
// Tips: Reflect.metadata 的结构
type metadata =
(
metadataKey: any,
metadataValue: any
) => {
<T extends (new(...args: any[]) => any)>(target: T): void;
(target: object, propertyKey: string | symbol): void;
}