Typescript: الوسيطة من النوع 'typeof FooBar' غير قابلة للتخصيص لمعامل من النوع 'new () => FooBarInterface'

تم إنشاؤها على ١٦ أبريل ٢٠١٥  ·  3تعليقات  ·  مصدر: microsoft/TypeScript

مرحبًا يا رفاق ، أنا أقوم بتطوير حاوية IoC لتطبيقات TypeScript تسمى TypeBinding<TServiceType> تستخدم لتعريف ارتباط بين service type (واجهة) و implementation type (فئة). رمز الفصل هو كالتالي:

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

لقد حددت بعض الكيانات لاختبار فئة 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;
  }
}

يمكنني استخدام فئة TypeBinding<TServiceType> بدون مشاكل مع الفصول Foo و Bar

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

تأتي مشكلتي عندما أحاول استخدام { new(): TServiceType ;} ومنشئ الفصل الذي ينفذ TServiceType ليس بدون معلمات.

الوسيطة من النوع "typeof FooBar" غير قابلة للتخصيص لمعامل من النوع "new () => FooBarInterface".

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

كيف يمكن التغلب على هذه المشكلة؟

شكرا :)

التعليق الأكثر فائدة

أعتقد أنني أصلحت المشكلة عن طريق تغيير سمة class:

implementationType : { new(): TServiceType ;},

ل:

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

سوف يكتب بعض اختبار وحدة يخدع تأكيد.

شكرا...

ال 3 كومينتر

أعتقد أنني أصلحت المشكلة عن طريق تغيير سمة class:

implementationType : { new(): TServiceType ;},

ل:

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

سوف يكتب بعض اختبار وحدة يخدع تأكيد.

شكرا...

ماهر! شكرا على هذا الشهي :)

شكرًا على هذا ، كنت أبحث عن طريقة لإجراء فحص للطباعة على المُنشئين الذين تم تمريرهم في معلمة دالة. سمح لي { new(...args : any[]): T ;} بالقيام بذلك.

مما يعني أنه يمكنني إجراء فحص للطباعة على أهداف المصمم

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

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات

القضايا ذات الصلة

siddjain picture siddjain  ·  3تعليقات

kyasbal-1994 picture kyasbal-1994  ·  3تعليقات

blendsdk picture blendsdk  ·  3تعليقات

DanielRosenwasser picture DanielRosenwasser  ·  3تعليقات

jbondc picture jbondc  ·  3تعليقات