如何模拟流类型兼容的 dom 类文件



我正在尝试获取摩卡规范来制造由流类型强制执行的File

以下产生^^^^^^ object literal. This type is incompatible with File.

正确的方法是什么?

/**
*
*   declare class Blob {
*    constructor(blobParts?: Array<any>, options?: {
*      type?: string;
*      endings?: string;
*    }): void;
*    isClosed: bool;
*    size: number;
*    type: string;
*    close(): void;
*    slice(start?: number, end?: number, contentType?: string): Blob;
*  }
*  
*   declare class File extends Blob {
*    constructor(
*      fileBits: $ReadOnlyArray<string | BufferDataSource | Blob>,
*      filename: string,
*      options?: FilePropertyBag,
*    ): void;
*    lastModifiedDate: any;
*    name: string;
*  }
*  
* @param name
* @param type
* @returns {*}
*/
function newFile (name: string, type?: string): File {
const result = { name, type }
return (result: File)
}

Flow 对类使用"名义类型"。这意味着仅具有相同键和值类型的对象不能代表该类的实际实例。我怀疑最好的办法是创建自己的File子类,覆盖您想要的字段/方法。如果重写字段/方法,则可能需要查看差异规则,以免从子类中获取类型错误。

最新更新