New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

reflect-helper

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reflect-helper

A utility library to for working with Angular2-style annotations and type information provided by TypeScript emitDecoratorMetadata.

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

This module is a utility library for working with Angular2-style annotations and type information provided by the TypeScript --emitDecoratorMetadata compiler option. An annotation is a class that can be added as metadata on a type, property, method, or parameter. API documentation is available here.

Usage

Suppose you have a class, EntityAnnotation, that can be used to flag a type as a persistent entity. The annotation can optionally specify the name of the entity. You can create this annotation in TypeScript as follows:

 class EntityAnnotation {

     constructor(public name?: string) {
     }
 }

To be able to apply the annotation to a class, we must first turn it into a decorator factory. We can do this using the makeDecorator function. When the decorator is applied to a class, an instance of the annotation is created and added to the 'annotations' metadata for the class.

The parameters for the decorator factory will be the same as the parameters for the class constructor.

 import { makeDecorator } from "reflect-helper";
 
 var Entity = makeDecorator(EntityAnnotation);

The Entity decorator can be applied to the class as demonstrated below. In this example we also apply a property decorator called Field:

 @Entity("person")
 class Person {
 
     @Field()
     age: number;

     constructor(age: number) {
         this.age = age;
     }
 }

If not using a transpiler, such as TypeScript or Babel, that supports decorators, we can instead apply the decorator using a helper function called decorate as follows:

var decorate = require("reflect-helper").decorate;

function Person(age) {
    this.age = age;
}

decorate(Person, Entity("person"), {
    age: Field()
});

Now at a later point in our program we want to check if the Person class is annotated with EntityAnnotation. We can do this using the getType function.

import { getType } from "reflect-helper";

var personType = getType(Person);

console.log(personType.hasAnnotation(EntityAnnotation)); // true
console.log(personType.getAnnotations(EntityAnnotation)[0].name); // person

Let's say we want to get the name and type of the first annotated property on the Person class:

var property = personType.properties[0];

console.log(property.name); // age
console.log(property.type.name); // Number 

Note that only properties that have an annotation are available in the [properties] array on the Type class. Also, the type of the property is only available if the --emitDecoratorMetadata compiler option is enabled in TypeScript.

Keywords

FAQs

Package last updated on 26 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc