Typing functions is the most useful thing you can do in TypeScript.
Direct answer
Annotate each parameter, and optionally the return type. Use ? or a default value for optional parameters, ...rest for variable arguments and void for functions that return nothing.
Examples
function add(a: number, b: number): number {
return a + b;
}
function greet(name: string, greeting = "Hello"): string {
return `${greeting}, ${name}`;
}
function log(message: string, level?: "info" | "error"): void {
console.log(level ?? "info", message);
}
function sum(...nums: number[]): number {
return nums.reduce((a, b) => a + b, 0);
}
Function types
type Callback = (error: Error | null, data?: string) => void;
const run = (cb: Callback) => cb(null, "done");
Where Teyro fits
Teyro's TypeScript for Beginners course turns this into short lessons you can play in a few minutes a day. It has 20 lessons, and you can start free.
The bottom line
Type parameters, let inference do the return type, annotate public functions.



