我遇到问题让Flow在不同文件中正确识别类型 . 如果我将所有代码放入单个文件中,请键入check pass,但是当各个类在单独的文件中并导入(ed)时,Flow会阻塞 . 有人找到了解决方法吗?
// FoundationTypes,js
type InstanceOfClassExtractor = <C>(Class<C>) => C;
type InstanceOf<Cls: Class<any>> = $Call<InstanceOfClassExtractor, Cls>;
type NullableExtractor = <T>(T) => T | null;
type Nullable<Obj: any> = $Call<NullableExtractor, Obj>;
export type id = Object | null;
// MyObject.js
//import type {id, InstanceOf} from "./FoundationTypes";
//import MyString from "./MyString";
//import MyException, {MyInvalidArgumentException} from "./MyException";
class MyObject {
static alloc(): Object {
return new this();
}
static new(): id {
return this.alloc().init();
}
init(): id {
return this;
}
}
// MyString.js
//import type {id, Nullable, InstanceOf} from "./FoundationTypes";
//import MyObject from "./MyObject";
//import MyException, {MyInvalidArgumentException} from "./MyException";
class MyString extends MyObject {
__jsString: string;
constructor(jsString?: string) {
super();
if (jsString !== null && jsString !== undefined)
this.__jsString = jsString;
}
static stringWithFormat(format: Nullable<InstanceOf<Class<MyString>>>, ...args: Array<any>): id {
return this.alloc().initWithFormat(format, ...args);
}
initWithFormat(format: Nullable<InstanceOf<Class<MyString>>>, ...args: Array<any>): id {
if (format === null) {
throw MyException.raise_format(MyInvalidArgumentException, new MyString("Nil value passed to initWithFormat:")
);
error, message: new MyString("Nil value passed to initWithFormat:") (Cannot call 'MyException.raise_format' with 'new MyString(...)' bound to 'format' because 'MyString' [1] is incompatible with 'MyString' [2].)
}
let self: id = super.init();
if (self) {
// %@ will always be a string, so convert, but objects must change to description
let formatString = format.__jsString.replace(/%@/, '%s');
let values: Array<number|string|boolean> = args.map(element => element instanceof MyObject ? element.description() : element);
//self.__jsString = sprintf(formatString, ...values);
}
return self;
}
}
export const MyInvalidArgumentException: InstanceOf<Class<MyString>> = new MyString("MyInvalidArgumentException");
export default class MyException extends MyObject {
__name: Nullable<InstanceOf<Class<MyString>>> = null;
__reason: Nullable<InstanceOf<Class<MyString>>> = null;
__userInfo: Nullable<InstanceOf<Class<MyObject>>> = null;
static exceptionWithName_reason_userInfo(name: Nullable<InstanceOf<Class<MyString>>>, reason: Nullable<InstanceOf<Class<MyString>>>, userInfo: Nullable<InstanceOf<Class<MyObject>>>): id {
const exception: id = this.new();
if (exception) {
exception.__name = name;
exception.__reason = reason;
exception.__userInfo = userInfo;
}
return exception
}
static raise_format(name: Nullable<InstanceOf<Class<MyString>>>, format: Nullable<InstanceOf<Class<MyString>>>, ...args: Array<any>): void {
const reason: Nullable<InstanceOf<Class<MyString>>> = MyString.stringWithFormat(format, ...args);
throw this.exceptionWithName_reason_userInfo(name, reason, null);
}
static raise_format_arguments(name: Nullable<InstanceOf<Class<MyString>>>, format: Nullable<InstanceOf<Class<MyString>>>, args: Array<any>): void {
this.raise_format(name, format, ...args);
}
}