如何模拟流类型兼容的dom类文件
问题描述:
我试图让摩卡规格制作File
由流类型强制执行。如何模拟流类型兼容的dom类文件
以下产量^^^^^^ 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)
}
答
流程使用"nominal typing"作为类。这意味着只有具有相同键和值类型的对象不能代表该类的实际实例。我怀疑最好的做法是让你自己的子类File
覆盖你想要的字段/方法。如果您重写字段/方法,您可能需要查看variance rules,以便您不会从您的子类中获取类型错误。
[Flow中的强制转换]可能的重复(https://stackoverflow.com/questions/41328728/force-casting-in-flow) – loganfsmyth