Babel:ES6中的函数参数类型 [英] Babel: Function parameter types in ES6

查看:477
本文介绍了Babel:ES6中的函数参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我编写以下代码并将其通过 Babel(6.5.0)进行编译,则它可以正常工作.

If I write the following piece of code and transpile it through Babel (6.5.0) it works correctly.

function foo (first: string, second: number) {
    // code here
}

从编译的ES5代码中删除了

: string: number.

: string and : number are just removed from the transpiled ES5 code.

如果我使用错误的参数类型调用该函数,则不会导致任何错误/警告.即使没有任何功能,它们也可以提供信息.

If I call the function using wrong parameter types, it does not result in any error/warning. They are informative even though do not have any functionality.

我在互联网上找不到有关ES6参数键入的正确信息.参数键入是否甚至是ES6的一部分?

I cannot find proper information about ES6's parameter typing on internet. Is parameter typing even part of ES6?

这个问题在下面的评论中得到了回答,我根据它们给出了正式的答案.

This question got answered in the comments below and I wrapped the official answer based on them.

推荐答案

感谢 Joe Clay Bergi Felix请在评论部分找到Kling .我将讨论中的答案包装在下面,因为没有人正式回答.

Thanks for Joe Clay, Bergi and Felix Kling for the answers in the comments section. I wrapped the answer below from the discussion as no-one answered officially.

-

似乎有些 Babel 插件(例如 babel-plugin-transform-flow-strip-types )会在编译时删除参数类型.我正在使用 babel-preset-react ,其中包括

It seems that some Babel plugins (eg. babel-plugin-transform-flow-strip-types) strip parameter types off while transpiling. I'm using babel-preset-react that includes babel-plugin-transform-flow-strip-types.

babel-plugin-transform-flow-strip-types 的示例行为从下面的

Example behaviour of babel-plugin-transform-flow-strip-types copy-pasted below from http://babeljs.io/docs/plugins/transform-flow-strip-types/

在:

function foo(one: any, two: number, three?): string {}

退出:

function foo(one, two, three) {}

结论,参数类型不是有效的ES6,但可以在以下情况下使用它们: 使用带剥离插件的Babel来翻译代码.

Conclusion, parameter types are not valid ES6, but them can be used if code is transpiled using Babel with the stripping plugins.

这篇关于Babel:ES6中的函数参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆