我正在编写供内部使用的私有(private) npm 包,我还想在其中包含一些流类型,这些流类型将在内部项目之间共享并以以下格式导入 import type {SomeType} from 'our-private-lib'
但是我在这样做时遇到了麻烦,将流类型包含在 npm 包中的最佳方法是什么?
目前我正在使用 babel 转译所有 ES 代码,然后还使用 flow-copy-source
将原始文件与转译后的文件一起复制过来,但使用 .js.flow
扩展名,但这意味着这些文件的名称应该与转译后的文件相同?
例如,如果我在 /super-schema/index.js
中有文件
export type SuperSchemaType = {
prop1: boolean,
prop2: string
}
const SuperSchema = {
prop1: true,
prop2: 'Hello'
}
module.exports = SuperSchema;
并且 package.json 指向主文件 index.js
,它像这样导出 SuperSchema
module.exports = {
superSchema: require('./super-schema.js/index.js');
}
然后我可以像这样导入
从'our-private-lib'导入{superSchema};
但是flowtype呢? import type { SuperSchemaType } from 'our-private-lib';
没有用
最佳答案
您的一般方法是正确的(使用 flow-copy-source),但如果您想使用模块主入口点之外的类型,则需要以某种方式导出类型。你可以通过做类似的事情来明确地做到这一点
export type {
SuperSchemaType
} from './super-schema';
或者,如果你使用的是 babel 7,export type * 可能对你有用
export type * from './super-schema';
关于javascript - FlowTypes 如何从私有(private) npm 模块导入类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49929148/