Functions That Return Nothing

Some functions don't return anything. They only do something, for example print a line. Their return type is called void (“emptiness”):

function printHero(name: string, health: number): void {
    console.log(`${name}: ${health} health`);
}

printHero("Kira", 50);
printHero("Bruno", 65);

This prints:

Kira: 50 health
Bruno: 65 health

: void says: this function returns no value, you call it for what it does. If you leave it out, TypeScript works out void by itself, but when you write it, your intention is clear, and TypeScript reports a return with a value that slipped in by mistake:

function printHero(name: string, health: number): void {
    console.log(`${name}: ${health} health`);
    return health;
}
main.ts(3,5): error TS2322: Type 'number' is not assignable to type 'void'.

A void function may still use return; without a value to end early.

In JavaScript, such a function returns undefined, and nothing stops you from using that “result” by mistake. It happens most often when a function prints a text that you actually need as a value. You know the difference between printing and returning from the JavaScript course:

function heroTitle(name: string, level: number) {
    console.log(`${name} of Raven Rock, level ${level}`);
}

const title = heroTitle("Kira", 3);
console.log(title.toUpperCase());
main.ts(6,19): error TS2339: Property 'toUpperCase' does not exist on type 'void'.

In JavaScript, this program would print the title and then crash with TypeError: Cannot read properties of undefined (reading 'toUpperCase'). TypeScript stops it before it runs: heroTitle returns void, so title holds nothing that could be used, and it has no properties. The fix is to let heroTitle return the text instead of printing it, with the return type string. Whoever calls it can then print the text, change it to capital letters or join it to another one.

Other uses of a void result are errors too, for example in a condition:

function printHero(name: string, health: number): void {
    console.log(`${name}: ${health} health`);
}

if (printHero("Kira", 50)) {
    console.log("Printed!");
}
main.ts(5,5): error TS1345: An expression of type 'void' cannot be tested for truthiness.

console.log(printHero("Kira", 50)) does get through, because console.log accepts anything. It prints the line and then undefined: a sign that you are printing the result of a function that returns nothing.

Typical mistakes:

  • A return with a value in a void function: Type 'number' is not assignable to type 'void'. Either the function should return the value (then change its return type), or the return doesn't belong there.
  • Printing inside a function whose result you need. If the code that calls the function needs the value, the function must return it.

Instructions

The function questName prints its text, but the program needs the text as a value. Change questName so that it returns the text instead of printing it. The program then prints:

Quest: Wolves in the Woods (50 gold)
THE LOST CROWN [LEVEL 3]

Now write the return types of both functions: void for printQuest and string for questName.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

3/10