2015-09-16 11 views
9

別のモジュールからインポートされた型定義がある場合、どのように再エクスポートしますか?別のファイルからインポートされたフロータイプ定義をどのようにエクスポートしますか?

/** 
* @flow 
*/ 

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

... 

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

代わりの './ExampleType' からExampleTypeとして 'インポートタイプ*書き込み;' を 書くことができます 'import type ExampleType from './ ExampleType ';' –

答えて

18

この質問の最も簡単な形式は、「どのようにタイプエイリアスをエクスポートするのですか?」です。簡単な答えは「export type!」です。

ご例えば

、あなたは

/** 
* @flow 
*/ 

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

「タイプの別名ExampleTypeである理由?」あなたが求めることができる書くことができますさて、あなたは

type MyTypeAlias = number; 

を書くときは、明示的numberのエイリアスタイプの別名MyTypeAliasを作成しています。あなたは

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

を書くとき、あなたは暗黙的にYourModule.jsYourTypeAlias輸出のエイリアスタイプの別名YourTypeAliasを作成しています。

+0

しかし、どのようにして簡単にすべての型を1行に再エクスポートできますか? –

+1

この記事によると、 'export type {ExampleType}'はサポートされている構文ではありません。 https://flowtype.org/blog/2015/02/18/Import-Types.html投稿の情報が最新ではありませんか?この構文はどこに文書化されていますか? https://flowtype.org/docs/modules.htmlにも記載されていません。 – Gajus

+0

FYI、 'babel-plugin-transform-flow-comments'に関するBabelのhttps://github.com/babel/babel/issues/5538に関連する問題を記入しました。 – Gajus

5

これは

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

編集取り組んでいる:は、受け入れられた答えは古いものと私の最後に警告を投げているreact-native-tab-view

1

で行われている方法から、この構文を取得します。ビューの量を考えると、フロー0.10+と互換性のある最新の回答があります。

MyTypes.js:

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

user.jsの:

import type {UserID, User} from "MyTypes"; 

    function getUserID(user: User): UserID { 
    return user.id; 
    } 

source

関連する問題