typescript / beginner
Snippet
Typed Function Parameters
TypeScript allows you to define explicit types for function arguments and the expected return type to prevent logic errors.
snippet.ts
1
2
3
function multiply(a: number, b: number): number {return a * b;}
Breakdown
1
a: number, b: number
Specifies that both 'a' and 'b' must be numbers when calling the function.
2
): number
Indicates that the function is guaranteed to return a number.