Hai teman-teman, saya sedang mengembangkan wadah IoC untuk aplikasi TypeScript yang disebut TypeBinding<TServiceType>
yang digunakan untuk mendefinisikan pengikatan antara service type
(antarmuka) dan implementation type
(kelas). Kode kelasnya adalah sebagai berikut:
// 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);
}
}
}
}
Saya telah mendefinisikan beberapa entitas untuk menguji kelas 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;
}
}
Saya dapat menggunakan kelas TypeBinding<TServiceType>
tanpa masalah dengan kelas Foo
dan Bar
var fooBinding = new TypeBinding<FooInterface>("FooInterface", Foo);
var barBinding = new TypeBinding<BarInterface>("BarInterface", Bar);
Masalah saya muncul ketika saya mencoba menggunakan { new(): TServiceType ;}
dan konstruktor kelas yang mengimplementasikan TServiceType
bukan tanpa parameter.
Argumen tipe 'typeof FooBar' tidak dapat ditetapkan ke parameter tipe 'baru () => FooBarInterface'.
var fooBarBinding = new TypeBinding<FooBarInterface>("FooBarInterface", FooBar);
Bagaimana cara mengatasi masalah ini?
Terima kasih :)
Saya pikir saya telah memperbaiki masalah ini dengan mengubah atribut class:
implementationType : { new(): TServiceType ;},
ke:
implementationType : { new(...args : any[]): TServiceType ;},
Akan menulis beberapa unit test con konfirmasi.
Terima kasih...
Cerdik! Terima kasih atas keceriaan ini :)
Terima kasih untuk ini, saya sedang mencari cara untuk melakukan pengecekan tipe pada konstruktor yang diteruskan dalam parameter fungsi. { new(...args : any[]): T ;}
mengizinkan saya melakukannya.
Yang berarti saya bisa melakukan pengecekan tipe pada target dekorator
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 { }
Komentar yang paling membantu
Saya pikir saya telah memperbaiki masalah ini dengan mengubah atribut class:
ke:
Akan menulis beberapa unit test con konfirmasi.
Terima kasih...