Perhaps there is a specific idiomatic rationale behind it that I am not aware of. Essentially, the code is defining an IMessagesOperation
type, which is a function that operates on an array of Message
objects and returns another array of Message
objects, like so:
type IMessagesOperation = (messages: Message[]) => Message[];
The reason for not structuring it this way could be related to how interfaces can be modified by additional declarations or the resolution of conflicts in type aliases using &
and extends
in TypeScript. It is possible to augment an interface by adding more declarations elsewhere in the code, as demonstrated below:
interface IMessagesOperation {
(messages: string): Message[];
// ^^^^^^−−−−−− note the change
}
This allows for function overloads where different types of inputs are accepted, making the code more flexible. On the other hand, such flexibility is not available when using a type alias like
type IMessagesOperation = (messages: Message[]) => Message[];
.
why Function
has capital "F" instead of just being function
The uppercase "F" in Function
refers to JavaScript's Function
constructor. In a type context, it signifies the type of object created by the constructor (i.e., a function).
How do I implement the concrete class in this case.
Since it is a function and not a class, you would implement it as follows:
const m1: IMessagesOperation = function(messages: Message[]): Message[] {
return messages;
};
Typescript playground example of the above