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: ... |
Get Modern Front-End Development for Rails, 2nd Edition 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.