@saber2pr/reflect

Typescript Reflector.

Usage no npm install needed!

<script type="module">
  import saber2prReflect from 'https://cdn.skypack.dev/@saber2pr/reflect';
</script>

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 的一个提案,它主要用来在声明的时候添加和读取元数据。

>> Reflect Metadata


API

  1. Reflect.defineMetadata

定义元数据

function defineMetadata(
  metadataKey: MetadataKey,
  metadataValue: MetadataValue,
  target: Object,
  propertyKey?: string
): void
  1. Reflect.getMetadata

获取元数据

function getMetadata<T>(
  metadataKey: MetadataKey,
  target: Object,
  propertyKey?: string
): T
  1. Reflect.getMetadataKeys

获取所有元数据键

function getMetadataKeys(target: Object, propertyKey?: string)
  1. Reflect.metadata

元数据装饰器

function metadata(metadataKey: MetadataKey, metadataValue: MetadataValue)

! tsconfig 需开启 experimentalDecorators: true

元数据设计键

! tsconfig 需开启 emitDecoratorMetadata: true

  1. design:type

  2. design:paramtypes

  3. design:returntype

>> Reflect 反射规则


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!