Hola chicos, estoy desarrollando un contenedor IoC para aplicaciones TypeScript llamado InversifyJS . Implementé una clase llamada TypeBinding<TServiceType>
que se usa para definir un enlace entre service type
(interfaz) y implementation type
(clase). El código de la clase es el siguiente:
// 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);
}
}
}
}
He definido algunas entidades para probar la clase 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;
}
}
Puedo usar la clase TypeBinding<TServiceType>
sin problemas con las clases Foo
y Bar
var fooBinding = new TypeBinding<FooInterface>("FooInterface", Foo);
var barBinding = new TypeBinding<BarInterface>("BarInterface", Bar);
Mi problema surge cuando trato de usar { new(): TServiceType ;}
y el constructor de la clase que implementa TServiceType
no es sin parámetros.
El argumento de tipo 'typeof FooBar' no se puede asignar al parámetro de tipo 'new () => FooBarInterface'.
var fooBarBinding = new TypeBinding<FooBarInterface>("FooBarInterface", FooBar);
¿Cómo puedo solucionar este problema?
Gracias :)
Creo que solucioné el problema cambiando el atributo de clase:
implementationType : { new(): TServiceType ;},
a:
implementationType : { new(...args : any[]): TServiceType ;},
Escribirá alguna prueba unitaria con confirmación.
Gracias...
¡Inteligente! Gracias por este tidbit :)
Gracias por esto, estaba buscando una forma de realizar la verificación de tipos en los constructores pasados en un parámetro de función. El { new(...args : any[]): T ;}
me permitió hacerlo.
Lo que significa que puedo realizar una verificación de tipo en los objetivos del decorador
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 { }
Comentario más útil
Creo que solucioné el problema cambiando el atributo de clase:
a:
Escribirá alguna prueba unitaria con confirmación.
Gracias...