FlowTypes 如何从私有 npm 模块导入类型

FlowTypes how to import types from private npm module

我正在编写供内部使用的私有 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');
}

然后我可以像这样导入

import {superSchema} from 'our-private-lib';

但是flowtype呢? import type { SuperSchemaType } from 'our-private-lib'; 真的不行

您的一般方法是正确的(使用 flow-copy-source),但是如果您想使用模块主入口点之外的类型,则需要以某种方式导出类型。您可以通过执行

之类的操作明确地做到这一点
export type {
  SuperSchemaType
} from './super-schema';

或者,如果您使用的是 babel 7,导出类型 * 可能对您有用

export type * from './super-schema';