Typescript无法从另一个模块访问导出的方法

时间:2015-09-29 13:56:10

标签: angularjs visual-studio typescript

无法从其他模块引用和使用导出的方法。收到错误消息“在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;
        }
    }

}

1 个答案:

答案 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
         }
    }
}
相关问题