最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Babel: Function parameter types in ES6 - Stack Overflow

programmeradmin3浏览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
}

: 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.

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

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

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
}

: 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.

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

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

Share Improve this question edited Mar 15, 2016 at 18:49 loganfsmyth 161k30 gold badges345 silver badges257 bronze badges asked Mar 10, 2016 at 12:46 CuriousSuperheroCuriousSuperhero 6,6714 gold badges29 silver badges52 bronze badges 7
  • 2 No, there is no type hinting in ES6 – rnevius Commented Mar 10, 2016 at 12:49
  • No, they are not ES6. They're most likely Typescript or Flow annotations. Check your transpiler settings. – Bergi Commented Mar 10, 2016 at 12:49
  • 1 @CuriousSuperhero - Bit of a shot in the dark, but are you using the React preset for Babel? Because that contains a plugin that strips type annotations. – Joe Clay Commented Mar 10, 2016 at 13:55
  • 1 Which contains the flow plugin: babeljs.io/docs/plugins/preset-react – Felix Kling Commented Mar 10, 2016 at 14:24
  • 1 @CuriousSuperhero - Then that's why you're not getting errors :) The syntax-flow and transform-flow-strip-types plugins included in babel-preset-react make it so errors are not thrown when type annotations are encountered, but do not actually process them - for that, you need to use the Flow tool itself. – Joe Clay Commented Mar 10, 2016 at 14:24
 |  Show 2 more comments

1 Answer 1

Reset to default 15

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.

--

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.

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

In:

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

Out:

function foo(one, two, three) {}

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

发布评论

评论列表(0)

  1. 暂无评论