简体   繁体   English

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

[英]FlowTypes how to import types from private npm module

I am writing private npm package to use internally, I also want to include some flowtypes in there that will be shared between internal projects and imported in following format import type {SomeType} from 'our-private-lib' But I am having trouble in doing so, what is the best approach to include flow types with npm package? 我正在编写私有的npm包以在内部使用,我也希望在那里包含一些将在内部项目之间共享的流import type {SomeType} from 'our-private-lib'并以下面的格式import type {SomeType} from 'our-private-lib'但是我遇到了麻烦这样做,使用npm包包含流类型的最佳方法是什么?

Currently I am transpiling all ES code with babel and then also using flow-copy-source to copy original files over along side transpiled files but with .js.flow extension, but then that means that the name of these files should be the same as the transpiled file? 目前我正在使用babel转换所有ES代码,然后使用flow-copy-source将原始文件flow-copy-source到侧面转换文件但扩展名为.js.flow ,但这意味着这些文件的名称应该与转换文件?

For example if I have file in /super-schema/index.js with 例如,如果我在/super-schema/index.js有文件

export type SuperSchemaType = {
 prop1: boolean,
 prop2: string
}

const SuperSchema = {
 prop1: true,
 prop2: 'Hello'
}

module.exports = SuperSchema;

And package.json points to main file index.js which exports SuperSchema like so 而package.json指向主文件index.js ,它SuperSchema像这样导出SuperSchema

module.exports = {
  superSchema: require('./super-schema.js/index.js');
}

I then can import that like so 然后我可以像这样导入它

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

but what about flowtype? 但是flowtype怎么样? import type { SuperSchemaType } from 'our-private-lib'; Doesn't really work 不起作用

Your general approach is correct (using flow-copy-source), but if you want to consume types out of the main entry point of your module, you need to export the types out one way or another. 您的一般方法是正确的(使用flow-copy-source),但如果您想要使用模块主入口点之外的类型,则需要以这种或那种方式导出类型。 You can do that explicitly by doing something like 你可以通过做类似的事情明确地做到这一点

export type {
  SuperSchemaType
} from './super-schema';

Or, if you're using babel 7, export type * might be useful for you 或者,如果您使用babel 7,导出类型*可能对您有用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM