こんにちはみんな私はInversifyJSと呼ばれるTypeScriptアプリ用のIoCコンテナを開発しています。 service type
(インターフェイス)とimplementation type
(クラス)の間のバインディングを定義するために使用されるTypeBinding<TServiceType>
という名前のクラスを実装しました。 クラスコードは次のとおりです。
// 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);
この問題をどのように回避できますか?
ありがとう :)
クラス属性を変更することで問題を修正したと思います。
implementationType : { new(): TServiceType ;},
に:
implementationType : { new(...args : any[]): TServiceType ;},
いくつかのユニットテストcon確認を書き込みます。
ありがとう...
頭がいい! この一口をありがとう:)
このおかげで、関数パラメーターで渡されたコンストラクターで型チェックを実行する方法を探していました。 { 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 { }
最も参考になるコメント
クラス属性を変更することで問題を修正したと思います。
に:
いくつかのユニットテストcon確認を書き込みます。
ありがとう...