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

Variables Without a Value

A variable created without a value, such as let reward: number;, has no value at first: in JavaScript, it holds undefined. The value is stored in it later, often in the branches of an if. TypeScript follows every path through the program and checks that wherever you use the variable, it surely has a value:

let monster = "goblin";
let reward: number;

if (monster === "wolf") {
    reward = 50;
} else if (monster === "goblin") {
    reward = 20;
}

console.log("Reward:", reward);
main.ts(10,24): error TS2454: Variable 'reward' is used before being assigned.

The message says that the variable is used before a value is assigned to it. But the monster is a goblin, so reward gets 20, doesn't it? This time, yes. TypeScript doesn't look at what monster holds right now, though. It checks every path the program could take. If the monster were a dragon, neither branch would run, and the program would print Reward: undefined. A calculation such as gold + reward would then give NaN.

To fix it, make sure that every path stores a value, most often with an else branch, which runs in all the other cases:

if (monster === "wolf") {
    reward = 50;
} else if (monster === "goblin") {
    reward = 20;
} else {
    reward = 0;
}

Another way is a starting value: let reward = 0;. The variable then has a value from the start, the branches only change it, and you don't even need the type, because TypeScript infers it from the 0. Choose what fits: a starting value when there is a sensible one, and a variable without a value when the value really comes only from the branches. TypeScript then checks that you haven't forgotten any of them.

Typical mistakes:

  • An if or else if without a final else.
  • A branch that does something else and forgets to store the value.
  • Using a variable that never gets a value at all: let reward: number; followed by console.log(reward); gives the same TS2454.

Instructions

  1. 1

    Run the program: TypeScript reports TS2454 twice. The else branch, for the large room, forgot to set the price. The large room costs 25 gold. Fix the branch so that the program prints:

    The best room in the house!
    Kira pays 25 gold. Gold left: 95
  2. 2

    Bruno also serves dinner, and it depends on the room too. Below price, create a variable meal of the type string without a value. Then store a value in it in each of the three branches: "bread" for the small room, "soup" for the medium one and "roast chicken" for the large one.

  3. 3

    At the end of the program, print the dinner, so that the program also prints:

    Dinner: roast chicken

    If TypeScript reports TS2454 for meal, check that every branch stores a value in it.

Back

3/11

  1. 1. Type Annotations
  2. 2. When to Write a Type
  3. 3. Variables Without a Value
  4. 4. Which Operations Are Allowed
  5. 5. Arrays
  6. 6. Quiz: Arrays
  7. 7. Tuples
  8. 8. Quiz: Tuples
  9. 9. Constants and Literal Types
  10. 10. any: Turning the Check Off
  11. 11. Summary
Next

Try it yourself

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