3

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?

1
  • A similar question has been handled in this q&a. Commented Dec 19, 2018 at 21:41

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.