Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Language

  • English
  • Čeština
  • Deutsch
  • Русский
Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Language

  • English
  • Čeština
  • Deutsch
  • Русский
Sign in

Objects in Functions

Object types are most useful in functions, as parameter types and return types. The first line of a function then tells the whole story: what the function takes and what it gives back.

A function that creates objects of one shape, often called a factory, gets the type as its return type. TypeScript then checks the object it returns:

interface Monster {
    name: string;
    level: number;
    health: number;
}

function createMonster(name: string, level: number): Monster {
    return { name: name, health: level * 10 };
}
main.ts(8,5): error TS2741: Property 'level' is missing in type '{ name: string; health: number; }' but required in type 'Monster'.

Without the return type, the function would return a monster without a level, and the mistake would show up only later, wherever a monster's level is read. With it, TypeScript reports the return itself.

Here is the working version, together with a function that changes a monster:

interface Monster {
    name: string;
    level: number;
    health: number;
}

function createMonster(name: string, level: number): Monster {
    return { name: name, level: level, health: level * 10 };
}

function enrage(monster: Monster): void {
    monster.level = monster.level + 2;
}

const wolf = createMonster("wolf", 3);
enrage(wolf);
console.log(wolf);

This prints:

{ name: "wolf", level: 5, health: 30 }

wolf gets the type Monster from the function's return type, without an annotation. enrage changes the very object that you pass to it, not a copy, so the change is visible outside the function, just as levelUp changed Kira's level in the exercise on read-only properties. Its return type is void: it doesn't return anything, the change is its result.

The calls are checked as with any other parameter type:

interface Monster {
    name: string;
    level: number;
    health: number;
}

function enrage(monster: Monster): void {
    monster.level = monster.level + 2;
}

enrage("wolf");
enrage({ name: "troll", level: 4, health: 40, angry: true });
main.ts(11,8): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Monster'.
main.ts(12,47): error TS2353: Object literal may only specify known properties, and 'angry' does not exist in type 'Monster'.

The first call passes a text instead of a monster. The second one passes an object literal with an extra property, as in the exercise on extra properties.

Typical mistakes:

  • A property missing from the returned object: Property 'level' is missing in type ..., reported at the return.
  • Passing something other than the object, such as the monster's name: Argument of type 'string' is not assignable to parameter of type 'Monster'.

Instructions

  1. 1

    Write a function createMonster with the parameters name (a string) and level (a number) and the return type Monster. It returns a new monster with that name and level, and with health equal to level * 10.

  2. 2

    With createMonster, create wolf, a wolf of level 2, and troll, a troll of level 5. Print both with describe. The program prints:

    wolf: level 2, 20 health
    troll: level 5, 50 health
  3. 3

    Write a function train with a parameter monster of type Monster and the return type void. It raises the monster's level by 1 and its health by 10.

  4. 4

    Train the wolf twice, and then print it with describe again. At the end, the program prints:

    wolf: level 4, 40 health
Back

9/10

  1. 1. Object Types
  2. 2. Type Aliases
  3. 3. Missing and Extra Properties
  4. 4. Optional Properties
  5. 5. Read-Only Properties
  6. 6. Interfaces
  7. 7. Nested Objects
  8. 8. Arrays of Objects
  9. 9. Objects in Functions
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.