So my question is, why the heck do I have to specify overtly meaningless names for function parameter parameters ?
I don't think they are meaningless. I can think of at least three good reasons why naming the parameters makes sense:
Consistency
This is how you define property types in TypeScript:
class Person {
public firstName: string;
public lastName: string;
public age: number;
}
This is how you specify variable types:
let person: Person;
Parameter types:
function meet(who: Person) {
}
Function and method return types:
function isUnderage(person: Person): boolean {
return person.age < 18;
}
This is how function type parameters would look without parameter names:
let myFunc: (string, string, number) => boolean;
...or...
function myFuncWithCallback(callback: (string, string, number) => boolean): void {}
...or...
type CallbackType = (string, string, number) => boolean;
let myFunc: CallbackType;
function myFuncWithCallback(callback: CallbackType): void {}
This doesn't quite fit with the other declarations above.
Throughout TypeScript, whenever you use types to signify static typing of a target you go with target: type
. That's easy to remember. If you start making rules like: Use the syntax target: type
in all cases except when defining parameters of function types, this makes your language less consistent and thus more difficult to learn and to use. It might technically not be necessary, but consistency is a value by itself. JavaScript is full of quirks and TypeScript is inheriting a lot of them. Better not introduce any additional inconsistencies.
So this is not an uncommon pattern and that is for good reasons.
this
parameters
Without specifying parameter names in function types, specifying this parameters in callback types would become even more messy and inconsistent. Consider this example from the linked page:
interface UIElement {
addClickListener(onclick: (this: void, e: Event) => void): void;
}
You would want it like this:
interface UIElement {
addClickListener(onclick: (this: void, Event) => void): void;
}
So then the rule would be "Use the syntax target: type
everywhere, except function types, where it is just the type in the parameters, unless there is a this
parameter, then it actually is that syntax, but only in the this
parameter." I don't blame the TypeScript designers to rather go with the rule "Use the syntax target: type
everywhere."
Development aids
This is what I get when I hover over a function in TypeScript:
How would you like it to read func: (number, Element) => any
instead of the descriptive parameter names it has? The type information is pretty useless by itself. And any code automatically generated from this definition would have to give the parameters meaningless names like param1
and param2
. That's obviously not optimal.
TypeScript is not the only language naming the paramters of function types:
C# delegates are defined like this:
delegate void EventHandler(object sender, EventArgs e);
Delphi function variables:
type TFunc = function(x: Integer, y: Integer): Integer;