我們常常會需要以模組為單位的定義、引用我們的型別 ( 以檔案為單位 )。
在 Flow 中可以匯出 type、interfaces、或是 class 等類型。
exports.js
// @flow
export type Person = {
name: string,
age: number,
};
export class Book {
// ...
}
export interface Swim {
doSwim(): void;
}
import.js
若要引入型別的話,要使用 import type 關鍵字來引入
// @flow
import type BookType, { Person, Swim } from './exports';
import Book from './my-modules'; // 因為要使用 new Book(),所以不能只使用 type
const book: BookType = new Book();
const person: Person = {
name: 'Wayne',
age: 26,
}
class Fish implements Swim {
doSwim() {
console.log('Fish Swim ~~~')
}
}