
Company News
/Security News
Socket Selected for OpenAI's Cybersecurity Grant Program
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.
@typinghare/ts-reflect
Advanced tools
At the very first beginning, we create two decorators, one of which is a class decorator and the other is a method decorator, as follows by using DecoratorGenerator. We can save some fields if needed. Those fields are named context in ts-reflect and can be accessed when decorators are applied. Notice that function myMethodDecorator accepts a parameter label then is passed to context. Users can custom the value of the label when applying this decorator.
// create.ts
import { DecoratorGenerator } from 'ts-reflect';
// create a decorator generator
const decoratorGenerator = new DecoratorGenerator();
// create a class decorator
function myClassDecorator(): ClassDecorator {
return decoratorGenerator.classDecorator({
// saves some fields
owner: 'James'
});
}
// create a method decorator
function myMethodDecorator(label: string): MethodDecorator {
return decoratorGenerator.methodDecorator({ label });
}
// export the decorators
export { myClassDecorator, myMethodDecorator };
We apply the decorators as follows. After a decorated class is loaded, its corresponding reflector can be obtained by the classContainer. Use getContext to access the context we save when creating the decorator. We can also obtain the reflector of the method myMethod. Similar operations of getting and setting the context of all kinds of reflectors are the same.
Prerequisite knowledge of typescript decorators is that they will be invoked when the generated js file is imported (or required). They will not be invoked again the second time of importing (or requiring).
// apply.ts
import { MyClassDecorator, myMethodDecorator } from './create';
import { classContainer } from 'ts-reflect';
// apply decorators to your class
@MyClassDecorator()
class MyClass {
@myMethodDecorator('My label')
public myMethod() {
// some codes
}
}
// get class context
const myClassReflector = classContainer.getByConstructor(MyClass);
console.log(myClassReflector?.getContext('owner')); // James
// get method context
const myMethodReflector = myClassReflector?.getMethod('myMethod');
console.log(myMethodReflector?.getContext('label')); // My label
FAQs
A useful library for making your own typescript decorators.
The npm package @typinghare/ts-reflect receives a total of 4 weekly downloads. As such, @typinghare/ts-reflect popularity was classified as not popular.
We found that @typinghare/ts-reflect 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.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.