Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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 类型脚本编译器不';不要抱怨这种一般性的限制 类存根 { } 界面调节器 { 方法(e:事件):无效; } 班级活动 { 目标:T; } 类侦听器实现IListener { 方法(e:事件):无效 { } }_Generics_Typescript_Constraints - Fatal编程技术网

Generics 类型脚本编译器不';不要抱怨这种一般性的限制 类存根 { } 界面调节器 { 方法(e:事件):无效; } 班级活动 { 目标:T; } 类侦听器实现IListener { 方法(e:事件):无效 { } }

Generics 类型脚本编译器不';不要抱怨这种一般性的限制 类存根 { } 界面调节器 { 方法(e:事件):无效; } 班级活动 { 目标:T; } 类侦听器实现IListener { 方法(e:事件):无效 { } },generics,typescript,constraints,Generics,Typescript,Constraints,TS编译器没有抱怨有什么原因吗?”“布尔”违反了接口约定。存根类型是在实现中指定的。我想这是因为存根类不期望任何基本上由其他类型(包括布尔示例)实现的东西。如果在存根中添加例如x:number,则会将其标记为错误。这是因为TypeScript是按结构键入的。以下工作(这是对代码示例的简化): class Stub { } interface IListener<T> { method( e : Event<T> ) : void; } class Event

TS编译器没有抱怨有什么原因吗?”“布尔”违反了接口约定。存根类型是在实现中指定的。

我想这是因为存根类不期望任何基本上由其他类型(包括布尔示例)实现的东西。如果在存根中添加例如x:number,则会将其标记为错误。

这是因为TypeScript是按结构键入的。以下工作(这是对代码示例的简化):

class Stub
{

}

interface IListener<T>
{
    method( e : Event<T> ) : void;
}

class Event<T>
{
    target : T;
}

class Listener implements IListener<Stub>
{
    method( e : Event<boolean> ) : void
    {

    }
}
但是,以下操作将失败:

class Stub
{

}

let foo:Stub = true; // Allowed ... `boolean` has all the member that a Stub has 
这样做的原因是开发人员方便

下面是更多的讨论:

class Stub
{
    someMember: string;
}

let foo:Stub = true; // Error. A boolean doesn't have `someMember` which should exist on a `Stub`