Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Typescript function type signatures include parameter names

Tags:

typescript

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?

like image 260
zacaj Avatar asked Dec 14 '25 05:12

zacaj


1 Answers

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. ...

-- TypeScript Handbook - Functions

You are correct that the names are not used.

like image 119
Alexander O'Mara Avatar answered Dec 16 '25 23:12

Alexander O'Mara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!