首页 > 解决方案 > 如何导出在打字稿中生成的导入命名空间?

问题描述

我的项目是一个微服务项目,它从文件中生成一些打字稿.proto(不相关)。我想创建一个本地 npm 包来导出这些文件中的内容,以及一些利用这些文件中的 types/etc... 的帮助函数。

我试过了:

// package/index.ts
const ns = require('./generated/namespace');
cosnt helper = (a: string) => ns.someHelper(a);
module.exports = { ns, helper };
// package/generated/namespace
export namespace ns {

    class SomeClass extends SomeType {
        // etc......
    }

    interface ISomeType {
        id?: (number|null)
        // etc...
    }

    // and so on...
}

// another_container/index.ts
import { ns } from '@our/package';

当我这样做并将其放入另一个容器时,我收到此错误:

类型 'typeof import("/path/to/another_container/node_modules/package/dist/index")' 上不存在属性 'ns'。

这甚至可能吗?

标签: javascripttypescriptmicroservicesnode-modulesjavascript-namespaces

解决方案


推荐阅读