导出的变量已经或正在使用私有类型的TypeScript错误

时间:2014-06-23 23:40:30

标签: typescript

以下内容会产生一个相当神秘的TypeScript构建错误:Exported variable 'res' has or is using private type 'Result'

interface Result {
    status: string;
    comment: string;
}

function runTest(st: any) {
    try {

    } catch (err) {
        console.log('Failed test task: ' + err);
        console.log('Failed test task: ' + st.name);
        console.log(err.stack);
        var res: Result = {
            status: 'bad',
            comment: 'Nodejs exception: ' + err,
        };
        //saveTestResult(st, res);
    }
};

export function what() {};

如果其中任何一个都可以:

  • 删除try / catch
  • 不要导出函数what

这里发生了什么?

1 个答案:

答案 0 :(得分:2)

您在编译器中发现了一个错误。你可以通过移动res的声明来解决它(这不会改变代码的行为):

function runTest(st: any) {
    var res: Result;
    try {

    } catch (err) {
        console.log('Failed test task: ' + err);
        console.log('Failed test task: ' + st.name);
        console.log(err.stack);
        res = {
            status: 'bad',
            comment: 'Nodejs exception: ' + err,
        };
        //saveTestResult(st, res);
    }
};