Giter Site home page Giter Site logo

saber2pr / reflect Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 21 KB

:eight_spoked_asterisk: Typescript Reflector.

Home Page: https://saber2pr.top/#/blog/Reflect%E5%8F%8D%E5%B0%84%E5%8E%9F%E7%90%86/reflect-metadata%E5%AE%9E%E7%8E%B0%E5%8F%8A%E5%85%B6%E5%8E%9F%E7%90%86

TypeScript 99.41% JavaScript 0.59%
typescript reflect metadata

reflect's Introduction

@saber2pr/reflect

Typescript Reflector.

PASS: 已通过所有官方测试用例

# from npm
npm install @saber2pr/reflect

# from github
git clone https://github.com/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!

reflect's People

Contributors

saber2pr avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.