2017-04-24 5 views
0

私は基本的にこのような何かやろうとしている:Typescript - ジェネリックでコンストラクタを定義するインタフェースを持つことは可能ですか?

interface gen1<T> { 
    constructor(param: T); 
} 
interface gen2<T> { 
    constructor(param: gen1<any>); 
} 
class genImpl implements gen2<any> { 
    constructor(param: gen1<any>) { 

    } 
} 

が、エラーを取得:

Class 'genImpl' incorrectly implements interface 'gen2<any>'. 
    Types of property 'constructor' are incompatible. 
    Type 'Function' is not assignable to type '(param: gen1<any>) => any'. 
     Type 'Function' provides no match for the signature '(param: gen1<any>): any'. 

答えて

2

インターフェイスのコンストラクタシグネチャはクラスで実装できません。これは設計によるものです。

When working with classes and interfaces, it helps to keep in mind that a class has two types: the type of the static side and the type of the instance side. You may notice that if you create an interface with a construct signature and try to create a class that implements this interface you get an error:

interface ClockConstructor { 
    new (hour: number, minute: number); 
} 

class Clock implements ClockConstructor { 
    currentTime: Date; 
    constructor(h: number, m: number) { } 
} 

This is because when a class implements an interface, only the instance side of the class is checked. Since the constructor sits in the static side, it is not included in this check.

Instead, you would need to work with the static side of the class directly. In this example, we define two interfaces, ClockConstructor for the constructor and ClockInterface for the instance methods. Then for convenience we define a constructor function createClock that creates instances of the type that is passed to it.

interface ClockConstructor { 
    new (hour: number, minute: number): ClockInterface; 
} 
interface ClockInterface { 
    tick(); 
} 

function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface { 
    return new ctor(hour, minute); 
} 

class DigitalClock implements ClockInterface { 
    constructor(h: number, m: number) { } 
    tick() { 
     console.log("beep beep"); 
    } 
} 
class AnalogClock implements ClockInterface { 
    constructor(h: number, m: number) { } 
    tick() { 
     console.log("tick tock"); 
    } 
} 

let digital = createClock(DigitalClock, 12, 17); let analog = 
createClock(AnalogClock, 7, 32); 

Because createClock’s first parameter is of type ClockConstructor, in createClock(AnalogClock, 7, 32), it checks that AnalogClock has the correct constructor signature.

関連の議論:documentationからhttps://github.com/Microsoft/TypeScript/issues/8917

+0

これは残念です...しかし、応答に感謝! – Brian

0

Is it possible to have generics in constructor?

はい

but getting error

理由はgen<T>で同じではありませんがas T

、すなわちこれまでTを用いたanyを使用
interface gen<T> { 
    constructor(param: T); 
} 

class genImpl implements gen<any> { 
    constructor(param: any) { 

    } 
} 

を修正します。 ( Tgen<any>anyの間に入れて間違えた)

+0

woops、悪い例、1秒、私はそれが – Brian

+0

が – Brian

+0

あなたは '書くclass'または宣言することもしかして、質問、その今の更新を急いで修正してみましょう'interface'、' new() '、' const'の組み合わせですか?コンストラクタは本質的に静的なので、これは機能しません。 –

関連する問題