Skip to main content
In this guide, we’ll explore functions in TypeScript by demonstrating how to make a duck quack. Elmer’s challenge is to get his ducks to quack, and we solve this by writing effective TypeScript functions.

Simple Function Implementation

The initial implementation of the function, named makeDuckQuack, logs a duck’s quack message. This function accepts a duck object and an optional parameter to specify the number of quacks. If the optional parameter is omitted, the duck quacks once by default.
In this code:
  • The first parameter is a duck object.
  • The second parameter, times, determines how many times the duck will quack.
The code uses a for loop with string interpolation to output the message.

Experimenting with Type Definitions

During development, a type definition for the duck and an example duck object were introduced. An incomplete function implementation caused compilation errors. Below is the snippet that initially led to errors:
The errors encountered were:
These errors occur due to an incomplete implementation of the function. Always ensure your function implementations are complete to avoid compilation issues.

Corrected Implementation

Below is the corrected version of the code with proper error handling and structure:
When calling the function as follows:
The console output will be:
This demonstrates how the function uses the optional parameter to control the quack count.

Using Default Parameter Values

We can simplify the function by using a default parameter value. This approach removes the need for the optional marker and manually setting a default value inside the function. Consider the following revised version:
When run with yarn dev, you’ll see:
Calling the function without the times parameter (i.e., makeDuckQuack(daffy)) will result in a single quack by default.

Function Styles in TypeScript

Another common way to define functions in TypeScript is by using arrow functions. Both traditional function declarations and arrow functions are valid. Below are examples of each style:

Traditional Function Declaration

Arrow Function Expression

Both styles accomplish the same task, so you can choose the approach that best fits your coding preferences.

Complete Working Example

Here’s a complete example using the simplified default parameter syntax:
Depending on the parameter provided, the output will be:
  • For makeDuckQuack(daffy):
  • For makeDuckQuack(daffy, 2):
The image is a timeline or flowchart with seven steps related to programming concepts, including "Variables & Parameters," "Arrays & Objects," "Types & Interfaces," "Functions," "Classes," "Union Types & Enums," and "Putting it All Together." The fourth step, "Functions," is highlighted.

Next Steps

In the next section, we will explore classes in TypeScript and how to apply object-oriented programming concepts to further improve your code structure. For more information on TypeScript and advanced function patterns, visit the TypeScript Documentation. Happy coding!

Watch Video