agent_bean
agent_bean

Reputation: 1575

unable to resolve signature of class decorator when called as an expression

I'm trying to follow this example. I can't get it to compile. Any ideas on how to resolve the issue?

import { Component } from '@angular/core';

function log(className)
{
  console.log(className)

  return (...args) => {
    console.log("Arguments passed to this class's constructor are ", args)
    return new className(...args)
  }
}

@log
class myExampleClass
{
    constructor(arg1, arg2)
    {
      console.log("Constructor fired!")
    }
}

const myClass = new myExampleClass(5,10)

The error i'm getting is.

Unable to resolve signature of class decorator when called as an expression.
  Type '(...args: any[]) => any' is not assignable to type 'typeof myExampleClass'.
    Type '(...args: any[]) => any' provides no match for the signature 'new (arg1: any, arg2: any): myExampleClass'.

Upvotes: 1

Views: 8205

Answers (1)

yurzui
yurzui

Reputation: 214017

The class decorator is applied to the constructor of the class and can be used to observe, modify, or replace a class definition.

In your example you're trying to replace a class definition. In that case you have to return type of the same class your @log decorator is applied to.

function log<T extends { new(...args: any[]): {} }>(className: T) {
  console.log(className)
  return class extends className {
    constructor(...args) {
      super(...args);
      console.log("Arguments passed to this class's constructor are ", args)
    }
  }
} 

See also:

Upvotes: 4

Related Questions