Interface ClassMethodDecoratorContext<This, Value>
interface ClassMethodDecoratorContext<This, Value> {
access: {
get(object: This): Value;
has(object: This): boolean;
};
kind: "method";
metadata: DecoratorMetadataObject;
name: string | symbol;
private: boolean;
static: boolean;
addInitializer(initializer: ((this: This) => void)): void;
}
access: {
get(object: This): Value;
has(object: This): boolean;
};
kind: "method";
metadata: DecoratorMetadataObject;
name: string | symbol;
private: boolean;
static: boolean;
addInitializer(initializer: ((this: This) => void)): void;
}
Type Parameters
- This = unknown
The type on which the class element will be defined. For a static class element, this will be the type of the constructor. For a non-static class element, this will be the type of the instance.
- Value extends ((this: This, ...args: any) => any) = ((this: This, ...args: any) => any)
The type of the decorated class method.
Properties
Readonly
access
An object that can be used to access the current value of the class element at runtime.
Type declaration
Readonly
kind
kind: "method"
The kind of class element that was decorated.
Readonly
metadata
Readonly
name
name: string | symbol
The name of the decorated class element.
Readonly
private
private: boolean
A value indicating whether the class element has a private name.
Readonly
static
static: boolean
A value indicating whether the class element is a static (true
) or instance (false
) element.
Methods
addInitializer
- add
Initializer (initializer): void Adds a callback to be invoked either before static initializers are run (when decorating a
static
element), or before instance initializers are run (when decorating a non-static
element).Returns void
Example
const bound: ClassMethodDecoratorFunction = (value, context) {
if (context.private) throw new TypeError("Not supported on private methods.");
context.addInitializer(function () {
this[context.name] = this[context.name].bind(this);
});
}
class C {
message = "Hello";
@bound
m() {
console.log(this.message);
}
}
Context provided to a class method decorator.