Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  52] [ 2]  / answers: 1 / hits: 16191  / 8 Years ago, thu, march 10, 2016, 12:00:00

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.


More From » reactjs

 Answers
1

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.


[#62988] Tuesday, March 8, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darrylm

Total Points: 499
Total Questions: 131
Total Answers: 108

Location: Saudi Arabia
Member since Mon, Sep 5, 2022
2 Years ago
;