Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does typescript allow to define a type for a lambda params?

Why it is not possible to do in ts something like this:

var stringArray: string[] = ["a", "b", "c"];
stringArray.map(str: string => console.log(str));
like image 732
karina Avatar asked Jan 05 '23 16:01

karina


1 Answers

var stringArray: string[] = ["a", "b", "c"];
stringArray.map(str: string => console.log(str));

Actually you can, you just need to take it into brackets:

stringArray.map((str: string) => console.log(str));

but in fact you can omit type declaration, because of type inference compiler already knows that str is a type of string:

stringArray.map(str => console.log(str));
like image 103
pleerock Avatar answered Jan 31 '23 00:01

pleerock