Suppose I define a function:
const nonEmpty = (str: string) => str.length > 0;
Now I'm adding a type to nonEmpty
like that:
const nonEmpty : (str: string) => boolean = (str: string) => str.length > 0;
It compiles but I don't like that the argument name (str
) appears twice. Moreover, the argument name does not make sense to add an argument name to the function type.
I would prefer const nonEmpty : string => boolean = ...
or similar. Is it possible in Typescript ?
P.S. I found another way to declare a function type:
const nonEmpty = (str: string): boolean => str.length > 0;
It looks more concise but a bit strange for an inexperienced Typescript developer. Would you prefer this type declaration ?
Feel free to omit second
string
type:Second option: