Use type annotations for function parameters and return type to keep the calling code inline and ensure the type checking within the function body.
function add(a: number, b: number): number {return a + b;}
add = function (x: number, y: number) {return x + y;};let add: (a: number, b: number) => number =function (x: number, y: number) {return x + y;};
Use the parameter?
type syntax to make a parameter optional.
Use the expression typeof(parameter) !== 'undefined'
to check if the parameter has been initialized.
function multiply(a: number, b: number, c?: number): number {if (typeof c !== 'undefined') {return a * b * c;}return a * b;}
Use default parameter syntax parameter:=defaultValue
if you want to set the default initialized value for the parameter.
Default parameters are optional.
To use the default initialized value of a parameter, you omit the argument when calling the function or pass
the undefined
into the function.
function applyDiscount(price: number, discount: number = 0.05): number {// ...}function applyDiscount(price: number, discount?: number): number {// ...}
in this tutorial, you will learn about the TypeScript rest parameters that allows you to represent an indefinite number of arguments as an array.
function getTotal(...numbers: number[]) : number {let total = 0;numbers.forEach((num) => total += num);return total;}
TypeScript function overloadings allow you to describe the relationship between parameter types and results of a function.
function add(a: number, b: number): number;function add(a: string, b: string): string;function add(a: any, b: any): any {return a + b;}
Quick Links
Legal Stuff
Social Media