Monorepo for packages published under the @decorators/* scope on JSR.
Creates aliases for existing class members, with support for methods, getters,
setters, auto-accessors, and fields. Static members are also supported, along
with #private members (in environments with support for ES2022 class fields).
Important
When working with private members, the @alias decorator must beß applied
inside of the same enclosing class that the member is declared in. This is
due to the way that private members are scoped in JavaScript.
Simplifies stack traces for improved debugging, improves code readability for a more maintainable codebase, and reduces the boilerplate typically associated with aliasing class members in TypeScript/JavaScript.
deno add @decorators/aliasnpx jsr add @decorators/aliasimport { alias } from "@decorators/alias";
class Foo {
// alias() can be used to create multiple aliases from one original member
@alias("qux", "nurp")
bar(): string {
return "baz";
}
// declare the aliased members to avoid compilation errors
declare qux: Foo["bar"];
declare nurp: Foo["bar"];
// or, use @alias.for on the alias itself and pass it the original member name.
@alias.for("bar")
baz(): string {
return this.bar();
}
}
const foo = new Foo();
console.assert(foo.bar === "baz"); // OK
console.assert(foo.bar === foo.baz); // OK
console.assert(foo.qux === foo.bar); // OK
console.assert(foo.nurp === foo.bar); // OKBind methods, getters, and setters to the appropriate context object, with support for static members and inheritance.
deno add @decorators/bindnpx jsr add @decorators/bindimport { bind } from "@decorators/bind";
class Foo {
@bind
bar(): Foo {
return this;
}
@bind
static self(): typeof Foo {
return this;
}
}
const { self } = Foo, { bar } = new Foo();
console.log(self === Foo); // true
console.log(bar() instanceof Foo); // trueHighly configurable LRU cache decorator for class methods, with support for TTL, max size, custom key generation, pre- and post-processing, lifecycle event handlers, and much more.
deno add @decorators/lrunpx jsr add @decorators/lruimport { lru } from "@decorators/lru";
class BasicExample {
@lru({ maxSize: 64, ttl: 1000 })
memoized(arg1: string, arg2: number): string {
return `${arg1}-${arg2}`;
}
}
const example = new BasicExample();
console.log(example.memoizedMethod("foo", 42)); // "foo-42"
console.log(example.memoizedMethod("foo", 42)); // "foo-42" (cached)Collection of type guard functions, decorator function signatures, decorator
factory signatures, and other utility types for working with both Stage 3 and
Legacy Decorators (Stage 2 / experimentalDecorators).
deno add @decorators/typesnpx jsr add @decorators/typesimport {
type AnyDecoratorArguments,
type AnyDecoratorReturn,
isDecoratorArguments,
isLegacyDecoratorArguments,
} from "@decorators/types";
function toStringTag<Args extends AnyDecoratorArguments>(
value: string,
): (...args: Args) => AnyDecoratorReturn<Args>;
// deno-lint-ignore no-explicit-any
function toStringTag(value: string): (...args: any[]) => any {
return (...args) => {
if (isDecoratorArguments(args)) {
const [target, context] = args;
if (context.kind !== "class") {
throw new TypeError(
`@toStringTag cannot decorate ${context.kind}s - it can only be used on the class itself.`,
);
}
context.addInitializer(function () {
Object.defineProperty(this.prototype, Symbol.toStringTag, {
value,
configurable: true,
});
});
} else if (isLegacyDecoratorArguments(args)) {
const [target] = args;
Object.defineProperty(target.prototype, Symbol.toStringTag, {
value,
configurable: true,
});
} else {
throw new TypeError("@toStringTag received invalid arguments");
}
};
}
// this decorator factory works in TS 4.x and 5.x without issue:
@toStringTag("Foo")
class Foo {
// ...
}Contributions are warmly welcomed! Please read the Contributing Guide for details on our code of conduct, and the process for submitting pull requests.
If you find a bug, please open an issue and we will get to it as soon as possible. Alternatively, if you feel up to fixing it yourself, please create the issue anyways (so we can track it) and submit a pull request with the fix!
- TC39 Decorators Proposal - The official TC39 proposal for decorators.
- Stage 3 Decorators in Deno - A microsite we created that's dedicated to cover the TC39 decorators proposal and its landmark implementation in Deno.
MIT © Nicholas Berlette. All rights reserved.
github · issues · jsr · docs · contributing