Adding Type Annotations to Functions

Functions also get to participate in the static-typing fun. The parameters and return values can have type annotations, and the function as a whole has its own static type.

Annotating Function Parameters

TypeScript function parameters can have type annotations, with a similar syntax to what we’ve seen for variable declarations. The annotations work whether the function is a named function, an anonymous function, or a method of a class. All three of these examples have the same typing:

 function priceOfTicket(row: number, accessType: string) : number { }
 
 let priceOfTicket = function(row: number, accessType: string) : number { }
 
 class Ticket {
  priceOfTicket(row: number, accessType: string) : number ...

Get Modern Front-End Development for Rails now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.