[英]Validating Object Map using Flow
I have the following: 我有以下几点:
// @flow import React from 'react'; type IconMap = { [name: string]: {} }; export const icons: IconMap = { circle: { viewbox: '0 0 473.66 473.66', path(props) { return ( <g id="Layer_2" data-name="Layer 2" {...props}> <g id="Layer_1-2" data-name="Layer 1"> <circle cx="236.83" cy="236.83" r="236.83" /> </g> </g> ); } } };
First, how can I make the type IconMap
above not accept an unsealed object as a value, but { viewbox: string, path: function }
instead? 首先,如何使上面的IconMap
类型不接受未密封的对象作为值,而是使用{ viewbox: string, path: function }
代替?
Second, if I'm trying to call icons
as follows: 其次,如果我尝试按以下方式调用icons
:
const obj = icons['circle']
How can I make sure the above fails at compile-time if circle
is not one of the keys of icons
? 如果circle
不是 icons
的键之一,如何确保以上内容在编译时失败?
For your first question you can do: 对于第一个问题,您可以执行以下操作:
type IconMap = {
[name: string]: {|
viewbox: string,
path: Object => React$Node
|}
};
For your second question, it's not possible to do that at compile time as far as I know. 对于第二个问题,据我所知,不可能在编译时执行此操作。
React$Node
is defined here , and ships with Flow by default. React$Node
在这里定义,默认情况下Flow附带。 Alternatively you could 或者,您可以
import type {Node} from 'react
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.