使用index.ts导出'default'

时间:2017-05-29 11:05:10

标签: javascript node.js typescript ecmascript-6 commonjs

我有以下项目结构:

build/
    build.ts
config/
    config.ts
    index.ts
...

config.ts包含默认的导出类型,如下所示:

export default {
    myProp: {
        someProp: "someValue"
    }
}

index.ts中的config/如下所示:

export * from './config';

现在我想在build.ts中导入配置类型,如下所示:

import config from '../config';

但在使用时(例如config.myProp),它告诉我myProp上不存在index.ts

根据官方模块文档here,这应该完全正常。我在这里错过了什么吗?

2 个答案:

答案 0 :(得分:5)

config/index.ts中重新导出配置:

export {default as config} from './config';

然后在build/build.ts

import {config} from '../config;

答案 1 :(得分:0)

由于潜在问题的数量,似乎有一种趋势是使用默认导出。建议使用命名出口。我本人很高兴遵循约定只使用命名的导出。 here所示的原因与我在该主题上的经验相符。

但是,如果您仍然选择默认导出,那么我认为您应该能够像这样在config/index.ts中重新导出它:

export {default} from './config';

然后在build/build.ts中,您应该可以进行

import config from '../config';