无法从其他模块引用和使用导出的方法。收到错误消息“在SecondModule中没有导出的成员”。
module FirstModule{
export class someClass{
constructor(method: SecondModule.exMethod) //Getting Error here: 'There is no exported member in SecondModule'
{}
}
}
module SecondModule{
export function exMethod(){
return function(input){
//do something
return result;
}
}
}
答案 0 :(得分:2)
您不能将函数引用用作类型;但是,您可以将构造函数限制为允许具有exMethod
签名的函数的特定函数类型。
以下是一个例子:
module FirstModule {
export class someClass {
constructor(method: () => (input) => any) {
}
}
}
module SecondModule {
export function exMethod() {
return function(input) {
// do something
return result;
}
}
}
new FirstModule.someClass(SecondModule.exMethod); // ok
new FirstModule.someClass(() => {}); // error
如果你想强制传递SecondModule.exMethod
,你可以跳过它并直接在someClass
中调用该函数:
module FirstModule {
export class someClass {
constructor() {
SecondModule.exMethod()(5); // example of calling it
}
}
}