Interface ClassMethodDecoratorContext<This, Value>

    Context provided to a class method decorator.

    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;
    }

    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

    access: {
        get(object: This): Value;
        has(object: This): boolean;
    }

    An object that can be used to access the current value of the class element at runtime.

    Type declaration

    • get:function
      • Gets the current value of the method from the provided object.

        Parameters

        Returns Value

        let fn = context.access.get(instance);
        
    • has:function
      • Determines whether an object has a property with the same name as the decorated element.

        Parameters

        Returns boolean

    kind: "method"

    The kind of class element that was decorated.

    name: string | symbol

    The name of the decorated class element.

    private: boolean

    A value indicating whether the class element has a private name.

    static: boolean

    A value indicating whether the class element is a static (true) or instance (false) element.

    Methods

    • 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).

      Parameters

      • initializer: ((this: This) => void)
          • (this): void
          • Parameters

            Returns void

      Returns void

      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);
      }
      }