2017-08-24 4 views
0

私はmocha仕様を取得して、フロータイプで実施するようにFileを作成しようとしています。フロータイプと互換性のある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) 
} 
+0

[フローでの強制キャスティング](https://stackoverflow.com/questions/41328728/force-casting-in-flow)の可能な複製 – loganfsmyth

答えて

0

フローでは、クラスには"nominal typing"が使用されます。これは、同じキーと値の型を持つオブジェクトをクラスの実際のインスタンスに使用することができないことを意味します。私はあなたが望むフィールド/メソッドをオーバーライドする独自のサブクラスをFileにするだけです。フィールド/メソッドをオーバーライドする場合は、variance rulesを確認して、サブクラスから型エラーが発生しないようにすることができます。