2016-11-28 11 views
0

反応-ネイティブfbsdk(v0.3.0)私はShareOpenGraphContentのインスタンスを作成するにはどうすればよい javascriptでエクスポートされた型定義のインスタンスを作成するにはどうすればよいですか?

export type ShareOpenGraphContent = { 
    /** 
    * The type of content to be shared is open graph content. 
    */ 
    contentType: 'open-graph', 

    /** 
    * Common parameters for share content; 
    */ 
    commonParameters?: ShareContentCommonParameters, 

    /** 
    * URL for the content being shared. 
    */ 
    contentUrl?: string, 

    /** 
    * Open Graph Action to be shared. 
    */ 
    action: ShareOpenGraphAction, 

    /** 
    * Property name that points to the primary Open Graph Object in the action. 
    */ 
    previewPropertyName: string, 
}; 

FBShareOpenGraphContent.js

に次の型を定義しますか?

答えて

1

種類はインスタンス化できるものではありません。フローを使用していると仮定すると、潜在的なタイプのエラーを確認するためにflowを実行した場合に役立ちます。また、いくつかのIDE(WebStormなど)でタイプに基づいた提案を表示することもできます。したがって、関数と変数宣言でそれらの型を使用することができます。例えば:あなたはタイプShareOpenGraphContentに準拠したオブジェクトを作成したい場合は、あなただけ定義する必要があり、あなたの元の質問については

var aNumber : Number = 10; 
aNumber.trim(); // This will raise an error when running flow, as Number does not have the trim method. 

function createOpenGraphContent(contentType: string, action: ShareOpenGraphAction, previewPropertyName : string) : ShareOpenGraphContent { 
    return { 
     contentType: contentType, 
     action: action, 
     previewPropertyName: previewPropertyName, 
     randomKey: 'something' // If you include this, flow will raise an error, since the definition of ShareOpenGraphContent does not include randomKey. 
    } 
} 

あなたは変数と似た何かを行うことができます必要なすべてのキーと必要な場合はオプションのキーですが、それ以外のキーはありません。とにかくあなたのコードは正常に動作しますが、フローは不平を言うでしょう。

異なるタイプベースのJavaScript(TypeScriptなど)を実行している場合は、本質的に同じになりますが、オプションでチェッカーを実行するのではなく、転送時にエラーが発生する可能性があります。

関連する問題