Oi pessoal, estou desenvolvendo um contêiner IoC para aplicativos TypeScript chamado TypeBinding<TServiceType>
que é usada para definir uma ligação entre uma service type
(interface) e uma implementation type
(classe). O código da classe é o seguinte:
// 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);
}
}
}
}
Eu defini algumas entidades para testar a classe 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;
}
}
Posso usar a classe TypeBinding<TServiceType>
sem problemas com as classes Foo
e Bar
var fooBinding = new TypeBinding<FooInterface>("FooInterface", Foo);
var barBinding = new TypeBinding<BarInterface>("BarInterface", Bar);
Meu problema surge quando tento usar { new(): TServiceType ;}
e o construtor da classe que implementa TServiceType
não é sem parâmetros.
O argumento do tipo 'typeof FooBar' não pode ser atribuído ao parâmetro do tipo 'new () => FooBarInterface'.
var fooBarBinding = new TypeBinding<FooBarInterface>("FooBarInterface", FooBar);
Como posso contornar esse problema?
Obrigado :)
Acho que resolvi o problema alterando o atributo de classe:
implementationType : { new(): TServiceType ;},
para:
implementationType : { new(...args : any[]): TServiceType ;},
Irá escrever algum teste de unidade con confirmar.
Obrigado...
Esperto! Obrigado por este petisco :)
Obrigado por isso, eu estava procurando uma maneira de realizar a verificação de tipos em construtores passados em um parâmetro de função. O { new(...args : any[]): T ;}
me permitiu fazer isso.
O que significa que posso realizar a verificação de tipos nos destinos do 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 { }
Comentários muito úteis
Acho que resolvi o problema alterando o atributo de classe:
para:
Irá escrever algum teste de unidade con confirmar.
Obrigado...