Creating methods and functions
One of the best ways used by TypeScript to improve the developer experience in Angular application development is through the ability to type parameters and return functions and methods.
Both for developers who create libraries and frameworks and for those who consume these software components, knowing what a function expects and what the expected return is allows us to reduce the time spent reading and looking for documentation, especially the runtime bugs that our system may encounter.
To carry out the typing of the parameters and the return of a function, let’s consider the following example:
interface invoiceItem { product: string; quantity: number; price: number; } type Invoice = Array<invoiceItem>; function getTotalInvoice(invoice: Invoice): number { let invoiceTotal = invoice.reduce( (total, item) => total + item.quantity * item.price, ...