【问题标题】:How do you export a Flow type definition that is imported from another file?如何导出从另一个文件导入的流类型定义?
【发布时间】:2015-12-12 16:58:09
【问题描述】:

给定一个从另一个模块导入的类型定义,你如何重新导出它?

/**
 * @flow
 */

import type * as ExampleType from './ExampleType';

...

// What is the syntax for exporting the type?
// export { ExampleType };

【问题讨论】:

  • 你可以写import type * as ExampleType from './ExampleType';而不是import type ExampleType from './ExampleType';

标签: javascript flowtype


【解决方案1】:

以下效果很好

export type { Type } from './types';

【讨论】:

    【解决方案2】:

    我刚刚发现需要单线来为 ES6 默认类执行此操作,基于 @locropulenton 的答案。假设你有

    // @flow
    
    export default class Restaurants {}
    

    Restaurants.js 文件中。要从同一目录中的 index.js 文件中导出它,请执行以下操作:

    export type {default as Restaurants} from './Restaurants';
    

    【讨论】:

      【解决方案3】:

      接受的答案是旧的,并且对我提出警告。鉴于观看次数,这是与 flow 0.10+ 兼容的更新答案。

      MyTypes.js:

        export type UserID = number;
        export type User = {
          id: UserID,
          firstName: string,
          lastName: string
        };
      

      用户.js:

        import type {UserID, User} from "MyTypes";
      
        function getUserID(user: User): UserID {
          return user.id;
        }
      

      source

      【讨论】:

        【解决方案4】:

        这个问题的最简单形式是“如何导出类型别名?”简单的答案是“export type!”

        对于你的例子,你可以写

        /**
         * @flow
         */
        
        import type * as ExampleType from './ExampleType';
        export type { ExampleType };
        

        你可能会问“为什么ExampleType 是一个类型别名?”好吧,当你写的时候

        type MyTypeAlias = number;
        

        您正在显式创建别名MyTypeAlias 的类型别名number。当你写的时候

        import type { YourTypeAlias } from './YourModule';
        

        您正在隐式创建类型别名YourTypeAlias,它为YourModule.jsYourTypeAlias 导出别名。

        【讨论】:

        • 但是如何在一行中轻松地重新导出所有类型?
        • 根据这篇文章,export type {ExampleType} 不是受支持的语法。 flowtype.org/blog/2015/02/18/Import-Types.html帖子里的信息是不是过期了?此语法记录在哪里? flowtype.org/docs/modules.html 中也没有记录。
        • 仅供参考,我已经向 Babel github.com/babel/babel/issues/5538 填写了关于 babel-plugin-transform-flow-comments 的相关问题。
        • 备案:@Gajus 链接到的页面底部有该语法的公告。我尝试了(Flow 0.46)并且它有效,该博客文章很旧。
        • 注意:export type * from './foo' 似乎不起作用。
        猜你喜欢
        • 2021-10-02
        • 2021-02-10
        • 2017-03-31
        • 2021-03-21
        • 2018-01-19
        • 1970-01-01
        • 1970-01-01
        • 2021-03-07
        • 1970-01-01
        相关资源
        最近更新 更多