在另一个模块文件

时间:2016-05-05 10:23:09

标签: typescript

这是模块A文件中定义的简单接口:

interface IDirectoryLink
{
    link_title: string
    link_desc: string
}

这是模块A文件中的类:

class DirectoryModel implements IDirectoryLink
{

    public link_title: string
    public link_desc: string

    constructor(fields: Object)
    {
        // Loops to populate class properties with fields values
        _.forOwn(fields, (value, key) => // Needs fat arrow to bind 'this'
        {
            if (value) { this[key] = value }
        })
    }
}

稍后在文件中,我实现了课程:

let directoryModel: IDirectoryLink = new DirectoryModel(fields)

然后仍然在模块A中,将实例传递给模块B中用于验证的函数:

let val = validation.modelValidator(directoryModel) 

在模块B文件中,这是传递模型实例以对其执行某些操作的函数的顶部:

export let modelValidator = function (directoryModel: IDirectoryLink) // cannot find name IDirectoryLink
{
//code
}

我希望传递的实例为'directoryModel',类型为IDirectoryLink。但它不是,它是“any”类型,如果我在上面的代码中添加':IDirectoryLink',它说“找不到名称IDirectoryLink”。

在模块B文件中将传递的实例识别为IDirectoryLink类型的正确方法是什么? 我应该在B中导入包含导出接口的整个模块A吗?

1 个答案:

答案 0 :(得分:0)

问题是在您的其他文件中,它不知道类型'IDirectoryLink'。您需要导入该类型。

这意味着你需要从模块A(接口类型和实现)导出多个东西,或者你需要有两个模块,一个包含你的接口(由模块B导入),另一个只包含你的实现。 / p>