function printId(id: number | string): void {
    console.log(`Your ID is: ${id}`);
}

printId(101);      // Output: Your ID is: 101
printId("202");    // Output: Your ID is: 202
Bash

Explanation:

  • This program uses union types to allow the id parameter in the printId function to accept either a number or a string.
  • Depending on the type passed to the function, it correctly prints the ID.
Resize text
Scroll to Top