Reputation: 2085
New to typescript, so maybe I'm missing something here...
I was trying to write a simple filter function on a container I'd made
class Container<T> {
filter(predicate: (T) => boolean): Container<T> {
for(const element of this.contents) {
if(predicate(element))
and tslint gave me an error about starting variables with capital letters (which is a rule I have on purpose). I wasn't sure what it meant at first, but apparently it's taking the T in (T) => boolean to be the name of the parameter, and not the type. After googling around for some typescript callback examples, I saw everyone typing a function signature as
(paramName: ParamType) => ReturnType.
But it seems like the paramName here is pointless. I'm not declaring the function here, I'm just giving its signature. Why is this valid Typescript?
Upvotes: 3
Views: 463
Reputation: 60577
The official reason appears to be to "help with readability"
A function’s type has the same two parts: the type of the arguments and the return type. When writing out the whole function type, both parts are required. We write out the parameter types just like a parameter list, giving each parameter a name and a type. This name is just to help with readability. ...
You are correct that the names are not used.
Upvotes: 3