Decorator Factory
unioc internally provides some decorator factories to create decorators that are compatible with both legacy
and stage3
environments.
Differences Between the Two Environments
In January 2023, TypeScript 4.9 was officially released, introducing TC39 stage 3
decorators to TypeScript for the first time. However, most IoC
frameworks are still at the legacy
stage and are not yet compatible with stage3
decorators. At this point in time, to promote the development of the TS
community, unioc internally provides decorator factory functions for creating decorators that are compatible with both legacy
and stage3
, as well as a series of metadata
scanners for setting/scanning metadata and a series of APIs compatible with emitDecoratorMetadata
(i.e., the reflect-metadata
package).
NOTE
The related APIs are stored in unioc/meta
and unioc/decorator
and support tree-shaking
. Even if you don't use the core container API of unioc
, you can use these APIs to create decorators, making your decorators more compatible and allowing them to be used in both legacy
and stage3
environments 🎉
This may be very useful for some decorator library developers, such as libraries that use decorators for data validation, etc.
defineClassDecorator
Used to define a unified class decorator.
import { defineClassDecorator } from 'unioc/decorator'
function MyClassDecorator() {
return defineClassDecorator({
onClassDecorate(target) {
// some your logic 🪄
}
})
}
@MyClassDecorator()
class MyClass {}