简体   繁体   English

javascript - ES6语法

[英]javascript - ES6 syntax

While checking the source of one open source project ( react data grid ), I've stumbled upon this syntax which I couldn't wrap my head around: 在检查一个开源项目( 反应数据网格 )的来源时,我偶然发现了这种语法,我无法解决这个问题:

class EditorBase extends React.Component {

  getStyle(): {width: string} {
    return {
      width: '100%'
    };
}


Especially the getStyle(): { part. 特别是getStyle(): { part。

I've tried to consult other SO questions, MDN function and classes references but didn't any mentions of the above syntax. 我试图咨询其他SO问题,MDN 函数引用,但没有提到上述语法。

What am I missing here? 我在这里错过了什么?

So this looks to be a usage of Flow-type (created by Facebook) which is a static type checker for Javascript. 所以这看起来是Flow-type(由Facebook创建)的用法,它是Javascript的静态类型检查器。 (Which you can think of like a Linter but for Data Types instead of syntax) (您可以将其视为Linter,但对于数据类型而不是语法)

So the part that looks confusing may be this bit: 所以看起来令人困惑的部分可能就是这样:

{width: string}

In flow, when you have a colon after the function's parenthesis, it will denote the expected type of return from that function. 在流程中,当您在函数的括号后面有一个冒号时,它将表示该函数的预期返回类型。

This code notifies Flow that the function should return an object that has a key of 'width' that should be of type 'string'. 此代码通知Flow该函数应返回一个具有“width”键的对象,该键应为“string”类型。

Check out Flow.org for more information, and how to get started using it yourself! 查看Flow.org了解更多信息,以及如何开始自己使用它!

The code you are referring to checks the types of variables being passed to a method. 您引用的代码检查传递给方法的变量类型。

Flow code/type checks have only use in development mode and will be stripped by babel. 流程代码/类型检查仅在开发模式下使用,并将被babel剥离。

Flow is a static type checker for JavaScript. Flow是JavaScript的静态类型检查器。

Read more about flow . 详细了解流量

Here is babel plugin that is being used for react-data-grid. 这是用于反应数据网格的babel插件 It is actually deprecated, you should instead use babel flow-runtime (in case you would like to use it). 它实际上已被弃用,您应该使用babel flow-runtime (如果您想使用它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM