模块类型
导入和导出类型
在模块(文件)之间共享类型通常很有用。在 Flow 中,您可以从一个文件导出类型别名、接口和类,并在另一个文件中导入它们。
exports.js
1export default class MyClass {};2export type MyObject = { /* ... */ };3export interface MyInterface { /* ... */ };
imports.js
import type MyClass, {MyObject, MyInterface} from './exports';
不要忘记在文件顶部添加
@flow
,否则 Flow 不会报告错误.
将值作为类型导入和导出
Flow 还支持使用typeof
导入其他模块导出的值的类型。
exports.js
1const myNumber = 42;2export default myNumber;3export class MyClass { /* ... */ };
imports.js
import typeof MyNumber from './exports';
import typeof {MyClass} from './exports';
const x: MyNumber = 1; // Works: like using `number`
与其他类型导入一样,这段代码可以被编译器剥离,因此它不会添加对其他模块的运行时依赖关系。