Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type for parameter in TypeScript Arrow Function

Tags:

typescript

With

"noImplicitAny": true

TypeScript will give the error:

Parameter 'x' implicitly has an 'any' type

for

.do(x => console.log(x));

and error

',' expected

for:

.do(x: any => console.log(x));
like image 247
RationalDev likes GoFundMonica Avatar asked Mar 27 '16 08:03

RationalDev likes GoFundMonica


People also ask

Can arrow functions have parameters?

Arrow functions can never have duplicate named parameters, whether in strict or non-strict mode.

What is the type of an arrow function?

ES6 version of TypeScript provides an arrow function which is the shorthand syntax for defining the anonymous function, i.e., for function expressions. It omits the function keyword. We can call it fat arrow (because -> is a thin arrow and => is a "fat" arrow).

How do you specify return type in TypeScript arrow function?

Set the return type of an arrow function in TypeScript # You can set the return type of an arrow function in TypeScript right after its parameters, e.g. const greet = (name: string): string => {} . Once a function's return type is set, the type checker alerts us if the function returns a value of a different type.

What is () => in TypeScript?

In a type position, => defines a function type where the arguments are to the left of the => and the return type is on the right. So callback: (result: string) => any means " callback is a parameter whose type is a function.


1 Answers

After some searching I found the correct way to define a type for the parameter is to add parentheses:

.do((x: any) => console.log(x));
like image 51
RationalDev likes GoFundMonica Avatar answered Oct 23 '22 16:10

RationalDev likes GoFundMonica