object.values
JavaScript typing utilities, like Flow and TypeScript, have become popular in JavaScript apps of all sizes. As I mentioned in our Script & Style Show typing podcast, typing is a great way to implicitly implement documentation and validation. Flow isn't always easy to perfect, however, and Object.values
was a pain point for me.
JavaScript键入实用程序(例如Flow和TypeScript)已在各种规模JavaScript应用程序中流行。 正如我在Script&Style Show键入播客中所提到的那样,键入是隐式实现文档和验证的好方法。 Flow并不总是那么容易完美,但是Object.values
对我来说是一个痛点。
When using Flow, Object.values
could trigger the following error:
使用Flow时, Object.values
可能触发以下错误:
Cannot call Object.values(…).map with function bound to callbackfn because property {prop} is missing in mixed [1] in the first argument.
无法使用绑定到callbackfn的函数来调用Object.values(…).map,因为在第一个参数的混合[1]中缺少属性{prop}。
The reason for this error is that Object.values()
could return any value type. One way to get past this annoyance is to use the following:
出现此错误的原因是Object.values()
可以返回任何值类型。 克服这种烦恼的一种方法是使用以下方法:
...(Object.values(whatever): any)
Using an any
type is never ideal but providing a type with Object.values
will help satisfy Flow. In the end, it does make sense that Object.values
isn't trusted, because anything could be returned, but having to use any
is a tough pill to swallow for type lovers!
使用any
类型都不是理想的选择,但是为类型提供Object.values
将有助于满足Flow。 最后,不信任Object.values
是有道理的,因为可以返回任何内容,但是必须使用any
很难使类型爱好者吞咽!
object.values