Babel: Function parameter types in ES6

前端 未结 1 589
萌比男神i
萌比男神i 2021-02-13 05:29

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) {         


        
1条回答
  •  既然无缘
    2021-02-13 05:58

    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 讨论(0)
提交回复
热议问题