我在弄清楚用于键入这种变形对象的语法时遇到了麻烦
const { height: deviceHeight, width: deviceWidth } = Dimensions.get("window");
现在,
deviceHeight
和deviceWidth
均已发现,都应为数字。 最佳答案
我想你想要这个:
const { height: deviceHeight, width: deviceWidth }: { height: number, width: number } = Dimensions.get("window");
通过将其插入如下所示,您可以看到它正确键入了
deviceHeight
和deviceWidth
:// @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)