具有功能的打字稿接口。不接受子类型作为实现接口的参数
Typescript Interface with function. Subtype as parameter not accepted for implementing the interface
我有一个 class 扩展另一个 class 如下所示
abstract class FooAbstract{
constructor(someProp:any){
this.someProp = someProp;
}
someProp:any;
}
class Foo extends FooAbstract{
constructor(prop:any){
super(prop);
}
someRandomFunction(){
console.log("Something")
}
}
我有一个界面,其功能如下所示
interface ExampleInterface{
someFunction: (foo:FooAbstract)=>any;
}
现在我想实现接口但是想在接口实现中将子类型作为函数someFunction的参数传递,如下所示
class Example implements ExampleInterface{
someFunction = (foo:Foo)=>{
console.log("Hello World");
}
}
Typescript 警告 someFunction 的实现不正确,并且类型 Foo 和 FooAbstract 不兼容。我想了解为什么我不能通过要求 FooAbstract
的子类型作为参数来实现函数 someFunction
其实这是有道理的,因为这样做不安全。考虑以下情况:
class Example implements ExampleInterface{
someFunction = (foo:Foo)=>{
console.log("Hello World");
foo.someRandomFunction() // we can call this since foo is of type Foo
}
}
class Boo extends FooAbstract{
constructor(prop:any){
super(prop);
}
// no someRandomFunction method
}
var ex: ExampleInterface = new Example();
ex.someFunction(new Boo({})) // ok, Boo is derived from FooAbstract
如果编译器允许您问题中的场景,上述代码会编译但在运行时失败,因为 someRandomFunction
在 Boo
上不存在。
您可以使接口通用,这样您就可以指定要使用的派生类型 FooAbsrtact
:
interface ExampleInterface< T extends FooAbstract >{
someFunction: (foo:T)=>any;
}
// now ok
class Example implements ExampleInterface<Foo>{
someFunction = (foo:Foo)=>{
console.log("Hello World");
foo.someRandomFunction()
}
}
class Boo extends FooAbstract{
constructor(prop:any){
super(prop);
}
// no someRandomFunction method
}
var ex: ExampleInterface<Foo> = new Example();
ex.someFunction(new Boo({})) // compile error as it should be
我有一个 class 扩展另一个 class 如下所示
abstract class FooAbstract{
constructor(someProp:any){
this.someProp = someProp;
}
someProp:any;
}
class Foo extends FooAbstract{
constructor(prop:any){
super(prop);
}
someRandomFunction(){
console.log("Something")
}
}
我有一个界面,其功能如下所示
interface ExampleInterface{
someFunction: (foo:FooAbstract)=>any;
}
现在我想实现接口但是想在接口实现中将子类型作为函数someFunction的参数传递,如下所示
class Example implements ExampleInterface{
someFunction = (foo:Foo)=>{
console.log("Hello World");
}
}
Typescript 警告 someFunction 的实现不正确,并且类型 Foo 和 FooAbstract 不兼容。我想了解为什么我不能通过要求 FooAbstract
的子类型作为参数来实现函数 someFunction其实这是有道理的,因为这样做不安全。考虑以下情况:
class Example implements ExampleInterface{
someFunction = (foo:Foo)=>{
console.log("Hello World");
foo.someRandomFunction() // we can call this since foo is of type Foo
}
}
class Boo extends FooAbstract{
constructor(prop:any){
super(prop);
}
// no someRandomFunction method
}
var ex: ExampleInterface = new Example();
ex.someFunction(new Boo({})) // ok, Boo is derived from FooAbstract
如果编译器允许您问题中的场景,上述代码会编译但在运行时失败,因为 someRandomFunction
在 Boo
上不存在。
您可以使接口通用,这样您就可以指定要使用的派生类型 FooAbsrtact
:
interface ExampleInterface< T extends FooAbstract >{
someFunction: (foo:T)=>any;
}
// now ok
class Example implements ExampleInterface<Foo>{
someFunction = (foo:Foo)=>{
console.log("Hello World");
foo.someRandomFunction()
}
}
class Boo extends FooAbstract{
constructor(prop:any){
super(prop);
}
// no someRandomFunction method
}
var ex: ExampleInterface<Foo> = new Example();
ex.someFunction(new Boo({})) // compile error as it should be