![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
reflect-helper
Advanced tools
A utility library to for working with Angular2-style annotations and type information provided by TypeScript emitDecoratorMetadata.
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.
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.
FAQs
A utility library to for working with Angular2-style annotations and type information provided by TypeScript emitDecoratorMetadata.
We found that reflect-helper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.