Search code examples
javascripttypescriptarrow-functions

How to write ES6 arrow function in TypeScript?


Definitely, TypeScript is very better for JavaScript projects, it has many benefits and one of them are typed variables, also arrow functions are awesome:

const arFunc = ({ n, m }) => console.log(`${n + m + 1}`);

The above code is a JavaScript simple arrow function with destructing assignment. but I cannot understand the implementation of it on TypeScript. it is so complicated and confused me.

Assignment type of interanced variables and outputted results. this is my desire.


Solution

  • You can declare the type of the parameter direclty after the parameter and the type of the return value before the arrow:

    ({ n, m } : { n: string, m: string } ) : void => console.log(`${n + m + 1}`)