Currently delving into TypeScript, and I've got a query regarding annotating function types.
Taking a look at this basic example:
export const add = (num1: number, num2: number):number => {
return num1 + num2;
};
This seems well-typed to me. Types are defined for the arguments and the return type of the function as well.
Now let's consider another valid syntax:
export const add2:(num1: number, num2: number) => number = (num1, num2) => {
return num1 + num2;
};
In this case, we're typing the variable add2
itself. It appears that my linter is satisfied with no missing return types in either case.
I could even combine both approaches:
export const add3:(num1: number, num2: number) => number = (num1: number, num2: number):number => {
return num1 + num2;
};
My question remains - what are the advantages or disadvantages of using approaches 1, 2, or 3 here? Is there a more commonly used style among them?