我有这个TypeScript代码:
import * as suman from 'suman';
const Test = suman.init(module,{
ioc: {
a: 'foo',
b: 'far'
} as suman.Ioc
});
正如您所看到的,我正在尝试声明ioc对象的类型为suman.Ioc(ioc对象应该"遵循Ioc接口")。但我的IDE说"找不到命名空间' suman'"。
如何在此方案中创建类型并在我的代码中引用它?如果可能,我希望能够从suman导入引用Ioc对象类型。
换句话说,我不想在同一个文件中执行此操作:
declare namespace suman {
interface Ioc {
a: string,
b: string
}
}
import * as suman from 'suman';
const Test = suman.init(module,{
ioc: {
a: 'foo',
b: 'far'
} as suman.Ioc
});
原因是因为我必须为这样的每个文件重复名称空间声明,这不应该是必要的(或建议的)。
答案 0 :(得分:1)
suman
已键入,但键入的版本尚未发布。
目前,如果您愿意使用最新代码,可以npm install sumanjs/suman
安装。
如果您想使用最新版本并使用打字,可以考虑使用typings
安装打字文件:typings install suman=github:sumanjs/suman/lib/index.d.ts
并在typings/index.d.ts
中添加tsconfig.json
至于as suman.Ioc
,这是一种告诉编译器的方法“嘿,我知道你认为这个'东西'属于其他类型,但我希望你把它当成'suman。 IOC'”。
这就是为什么,即使那里有打字,也不会做你想做的事。
幸运的是,suman
提供的打字将适合您。