流,类型的破坏对象

时间:2018-03-02 17:35:14

标签: javascript reactjs react-native flowtype

我无法找出输入这种类型的析构对象的语法

const { height: deviceHeight, width: deviceWidth } = Dimensions.get("window");

现在deviceHeightdeviceWidth被揭露,两者都应该是数字。

1 个答案:

答案 0 :(得分:2)

我想你想要这个:

const { height: deviceHeight, width: deviceWidth }: { height: number, width: number } = Dimensions.get("window");

您可以通过将其插入以下内容来正确输入deviceHeightdeviceWidth

// @flow
const { height: deviceHeight, width: deviceWidth }: { height: number, width: number } = { height: 1, width: 2 };

function logString(str: string) {
  console.log(str);
}

logString(deviceHeight);

...给出了这个输出:

logString(deviceHeight);
          ^ Cannot call `logString` with `deviceHeight` bound to `str` because number [1] is incompatible with string [2].

See it on flow.org