Typescript: Argument vom Typ 'typeof FooBar' kann dem Parameter vom Typ 'new() => FooBarInterface' nicht zugewiesen werden

Erstellt am 16. Apr. 2015  ·  3Kommentare  ·  Quelle: microsoft/TypeScript

Hallo Leute, ich entwickle einen IoC-Container für TypeScript-Apps namens InversifyJS . Ich habe eine Klasse namens TypeBinding<TServiceType> implementiert, die verwendet wird, um eine Bindung zwischen einer service type (Schnittstelle) und einer implementation type (Klasse) zu definieren. Der Klassencode lautet wie folgt:

// Defines allowed scope modes
enum TypeBindingScopeEnum {
  Transient,
  Singleton
}

class TypeBinding<TServiceType> {

    // The runtime identifier used because at runtime
    // we don't have interfaces
    public runtimeIdentifier : string;

    // The constructor of a class that must implement TServiceType
    public implementationType : { new(): TServiceType ;};

    // Cache used to allow singleton scope
    public cache : TServiceType;

    // The scope mode to be used
    public scope : TypeBindingScopeEnum;

    constructor(
      runtimeIdentifier : string,
      implementationType : { new(): TServiceType ;},
      scopeType? : TypeBindingScopeEnum) {

      this.runtimeIdentifier = runtimeIdentifier;
      this.implementationType = implementationType;
      this.cache = null;
      if(typeof scopeType === "undefined") {
        // Default scope is Transient
        this.scope = TypeBindingScopeEnum.Transient;
      }
      else {
        if(TypeBindingScopeEnum[scopeType]) {
            this.scope = scopeType;
        }
        else {
          var msg = `Invalid scope type ${scopeType}`;
          throw new Error(msg);
        }
      }
    }
}

Ich habe einige Entitäten zum Testen der Klasse TypeBinding<TServiceType> :

interface FooInterface {
  logFoo() : void;
}

interface BarInterface {
  logBar() : void;
}

interface FooBarInterface {
  logFooBar() : void;
}

// Notice default constructor
class Foo implements FooInterface {
  public logFoo(){ 
    console.log("foo"); 
  }
}

// Notice default constructor
class Bar implements BarInterface {
  public logBar(){ 
    console.log("bar"); 
  }
}

// Notice dependencies on FooInterface and  BarInterface on constructor
class FooBar implements FooBarInterface {
  public foo : FooInterface;
  public bar : BarInterface;
  public logFooBar(){ 
    console.log("foobar"); 
  }
  constructor(FooInterface : FooInterface, BarInterface : BarInterface) {
    this.foo = FooInterface;
    this.bar = BarInterface;
  }
}

Ich kann die Klasse TypeBinding<TServiceType> ohne Probleme mit den Klassen Foo und Bar

var fooBinding = new TypeBinding<FooInterface>("FooInterface", Foo);
var barBinding = new TypeBinding<BarInterface>("BarInterface", Bar);

Mein Problem tritt auf, wenn ich versuche, { new(): TServiceType ;} und der Konstruktor der Klasse, die TServiceType implementiert, nicht parameterlos ist.

Argument vom Typ 'typeof FooBar' kann dem Parameter vom Typ 'new() => FooBarInterface' nicht zugewiesen werden.

var fooBarBinding = new TypeBinding<FooBarInterface>("FooBarInterface", FooBar);

Wie kann man dieses Problem umgehen?

Danke :)

Hilfreichster Kommentar

Ich glaube, ich habe das Problem behoben, indem ich das Klassenattribut geändert habe:

implementationType : { new(): TServiceType ;},

zu:

implementationType : { new(...args : any[]): TServiceType ;},

Werde einige Unit-Tests schreiben, die bestätigen.

Danke...

Alle 3 Kommentare

Ich glaube, ich habe das Problem behoben, indem ich das Klassenattribut geändert habe:

implementationType : { new(): TServiceType ;},

zu:

implementationType : { new(...args : any[]): TServiceType ;},

Werde einige Unit-Tests schreiben, die bestätigen.

Danke...

Clever! Danke für diesen Leckerbissen :)

Vielen Dank dafür, ich habe nach einer Möglichkeit gesucht, Typprüfungen für Konstruktoren durchzuführen, die in einem Funktionsparameter übergeben werden. Die { new(...args : any[]): T ;} mir das erlaubt.

Das bedeutet, dass ich eine Typprüfung von Dekoratorzielen durchführen kann

function decor(oPar: {}): <TFunc extends { new (...args: any[]): A }>(target: TFunc) => void {    
    return function <TFunc extends { new (...args: any[]): A }>(target: TFunc): void {
        console.log(oPar, target);
    }
}

class A { id: string; }
// will error
@decor({})
class B { }

// Works
@decor({})
class C extends A { }

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen