Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Generics Typescript-是否可以让接口用泛型定义构造函数?_Generics_Typescript - Fatal编程技术网

Generics Typescript-是否可以让接口用泛型定义构造函数?

Generics Typescript-是否可以让接口用泛型定义构造函数?,generics,typescript,Generics,Typescript,我基本上是想做这样的事情: interface gen1<T> { constructor(param: T); } interface gen2<T> { constructor(param: gen1<any>); } class genImpl implements gen2<any> { constructor(param: gen1<any>) { } } 接口gen1{ 构造函数(参数:T

我基本上是想做这样的事情:

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

    }
}
接口gen1{
构造函数(参数:T);
}
接口gen2{
构造函数(参数:gen1);
}
类genImpl实现了gen2{
构造函数(参数:gen1){
}
}
但是得到错误:

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'.
类“genImpl”不正确地实现了接口“gen2”。
属性“构造函数”的类型不兼容。
类型“函数”不可分配给类型“(参数:gen1)=>any”。
类型“Function”不提供与签名(param:gen1:any)匹配的内容。
在构造函数中可以有泛型吗

但是得到了错误

原因是
gen
T

修理
接口生成器{
构造函数(参数:T);
}
类genImpl实现gen{
构造函数(参数:任意){
}
}

i、 e.在使用
T
的地方使用
any
。(您错误地将
T
同时放在
gen
any
)接口中的构造函数签名不能在类中实现。这是故意的。从:

在使用类和接口时,请记住 一个类有两种类型:静态端类型和 实例端的。您可能会注意到,如果您创建了一个接口 并尝试创建一个实现 此界面将显示一个错误:

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

class Clock implements ClockConstructor {
    currentTime: Date;
    constructor(h: number, m: number) { }
}
这是因为当类实现接口时,只有 类的实例端已选中。因为构造函数位于 静态侧,不包括在本检查中

相反,您需要处理类的静态方面 直接的。在这个例子中,我们定义了两个接口,ClockConstructor 用于实例方法的构造函数和ClockInterface。然后 为了方便起见,我们定义了一个构造函数createClock 创建传递给它的类型的实例

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); 
因为createClock的第一个参数 是ClockConstructor类型,在createClock(AnalogClock,7,32)中 检查AnalogClock是否具有正确的构造函数签名


相关讨论:

您的意思是编写
声明类
还是将
接口
新建()
常量
组合在一起?这将不起作用,因为构造函数本质上是静态的。@basarat您是我的typescript英雄,但这完全是错误的。您的代码示例正好引发了问题中的错误。
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);