【发布时间】:2018-07-18 00:25:59
【问题描述】:
是否可以将函数签名分配给名称?
例如,一些外部模块提供了接口
interface ExtFunctionInterface {
(arg1: string, arg2: number): boolean
}
我想创建一个本地接口或抽象类,将上述接口签名分配给方法名称。例如:
interface MyLocalInterface {
someMethodName typeof ExtFunctionInterface
}
或
abstract class MyLocalAbstractClass {
public abstract someMethodName typeof|signature of ExtFunctionInterface
}
这样当实现接口或扩展类时:
class MyClass {implements|extends} MyLocal{Interface|AbstractClass} {
// Correct impl
public someMethodName(arg1: string, arg2: number): boolean {
// impl
}
// Compiler would complain that the function is not implemented
// correctly because "arg2" is missing or type is wrong for e.g.
public someMethodName(arg1: string): boolean {
// impl
}
}
这意味着当 ExtFunctionInterface 改变时,编译器会通知签名不正确。
【问题讨论】:
标签: class typescript interface abstract-class typescript-typings