@saber2pr/reflect
v0.0.9
Published
> Typescript Reflector.
Downloads
8
Readme
@saber2pr/reflect
Typescript Reflector.
PASS: 已通过所有官方测试用例
# from npm
npm install @saber2pr/reflect
# from github
git clone https://github.com/Saber2pr/-saber2pr-reflect.git
Reflect-metadata
@Reflect.metadata('inClass', 'A')
class Test {
constructor(private test?: Test) {}
@Reflect.metadata('inMethod', 'B')
public hello(): string {
return 'hello world'
}
}
console.log(Reflect.getMetadata('inClass', Test)) // 'A'
console.log(Reflect.getMetadata('design:paramtypes', Test)) // [ [Function: Test] ]
console.log(Reflect.getMetadata('inMethod', new Test(), 'hello')) // 'B'
What is Reflect-metadata?
Reflect Metadata 是 ES7 的一个提案,它主要用来在声明的时候添加和读取元数据。
API
- Reflect.defineMetadata
定义元数据
function defineMetadata(
metadataKey: MetadataKey,
metadataValue: MetadataValue,
target: Object,
propertyKey?: string
): void
- Reflect.getMetadata
获取元数据
function getMetadata<T>(
metadataKey: MetadataKey,
target: Object,
propertyKey?: string
): T
- Reflect.getMetadataKeys
获取所有元数据键
function getMetadataKeys(target: Object, propertyKey?: string)
- Reflect.metadata
元数据装饰器
function metadata(metadataKey: MetadataKey, metadataValue: MetadataValue)
! tsconfig 需开启 experimentalDecorators: true
元数据设计键
! tsconfig 需开启 emitDecoratorMetadata: true
design:type
design:paramtypes
design:returntype
start
npm install
npm start
npm test
Author: saber2pr
develope and test
you should write ts in /src
you should make test in /src/test
export your core in /src/index.ts!