0

react-native-fbsdk (v0.3.0) 在 FBShareOpenGraphContent.js 中定义了以下类型

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,
};

如何创建ShareOpenGraphContent的实例?

4

1 回答 1

1

类型不是您可以实例化的东西。假设您使用的是流,它们仅在您运行flow以检查潜在的类型错误时才有用。它们还使某些 IDE(如 WebStorm)能够根据类型向您显示建议。因此,您可以在函数和变量声明中使用这些类型。例如:

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.
    }
}

您可以对变量执行类似的操作:

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

至于你原来的问题,如果你想创建一个符合 ShareOpenGraphContent 类型的对象,你只需要定义所有必需的键,如果你需要它们,是可选的,但别无其他。无论如何,您的代码都会运行良好,但流程会抱怨。

如果您正在运行不同的基于类型的 JavaScript(例如 TypeScript),则归结为基本相同,只是在编译时可能会出现错误,而不是在可选地运行检查器时出现错误。

于 2016-11-28T21:47:27.613 回答