"as" 使用 TypeScript 的符号,需要导出命名空间?

"as" notation with TypeScript, need to export a namespace?

我有这个 TypeScript 代码:

import * as suman from 'suman';

const Test = suman.init(module,{
    ioc: {
        a: 'foo',
        b: 'far'
    } as suman.Ioc
});

如您所见,我正在尝试声明 ioc 对象的类型为 suman.Ioc(ioc 对象应为 "adhere to the Ioc interface")。但是我的 IDE 说 "cannot find namespace '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  
    });

原因是因为我将不得不为每个这样的文件重复命名空间声明,这是不必要的(或建议的)。

suman 已输入,但输入的版本尚未发布。

目前,如果您愿意使用最新代码,可以通过 npm install sumanjs/suman 安装它。

如果您想使用最新版本并使用 typings,您可以考虑使用 typings 安装 typings 文件:typings install suman=github:sumanjs/suman/lib/index.d.ts 并在您的 [=15] 中包含 typings/index.d.ts =].

至于as suman.Ioc,是一种告诉编译器"hey, I know that you think this 'thing' is of some other type, but I would like you to treat it as 'suman.Ioc'".

的方式

这就是为什么,即使打字在那里,它也不会做你想要的。

幸运的是,suman 提供的类型对您来说可以正常工作。