
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
@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 and then is passed to context. Users can customize the value of the label when applying this decorator.
// create.ts
import { DecoratorGenerator, getClass, Zone } from '../src/main'
// create a zone and a decorator generator
export const myZone = new Zone()
export const DG = new DecoratorGenerator(myZone)
// create a context for your class decorator
export type MyClassDecoratorContext = { owner: string }
// create a context for your method decorator
export type MyMethodDecoratorContext = { label: string }
// create a class decorator
export const MyClassDecorator = function (): ClassDecorator {
return DG.generateClassDecorator({
// saves some fields
owner: 'James',
})
}
// create a method decorator
export const MyMethodDecorator = function (label: string): MethodDecorator {
return DG.generateMethodDecorator({ label })
}
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.
Note that in TypeSrcipt, decorators (as functions) will be called when the generated JavaScript file is imported (or required). They will not be invoked again at the second time of importing (or requiring).
// apply.ts
// apply decorators to your class
@MyClassDecorator()
class MyClass {
@MyMethodDecorator('my_label')
public myMethod() {
// some codes
}
}
it('Simple test.', function () {
// get class context
const myClassReflector = getClass<MyClassDecoratorContext>(MyClass)
const owner = myClassReflector!.getContext(myZone, 'owner')
expect(owner).toBe('James')
// get method context
const myMethodReflector = myClassReflector!.getMethod<MyMethodDecoratorContext>('myMethod')
const label = myMethodReflector!.getContext(myZone, 'label')
expect(label).toBe('my_label')
})
FAQs
A useful library for making your own typescript decorators.
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.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.