是否可以在文件中定义类类型,该类型在另一个文件中明确导入?
例如:
types.js
export type MyType {
id: number,
name: string,
};
declare class MyOject {
constructor(): MyObject;
getStuff(param: number): MyType;
...
}
main.js
import type {MyObject, MyType} from './types.js'; // <- flow does now recognize MyObject
....
我希望能够像main.js
一样导入它,但这违反了流程,因为它不会将MyObject
识别为有效导入。
我尝试了几种不同的解决方案但没有成功:
declare class
更改为export class
会导致流量错误有没有办法定义流类类型并从它定义的文件中明确导入?
答案 0 :(得分:1)
您想要使用declare export class
:
Types.js
// @flow
export type MyType = {
id: number,
name: string,
}
declare export class MyObject {
constructor(): void;
getStuff(param: number): MyType;
}
Main.js
// @flow
import {MyObject} from './types.js'
import type { MyObject as MyObjectType, MyType } from './types.js'
const newObj: MyObjectType = new MyObject()
作为回购:https://github.com/jameskraus/flow-exporting-declared-class