myHotTake

Tag: TypeScript variables

  • How Does TypeScript Enhance JavaScript Variable Safety?

    If you enjoyed this story, feel free to like or share it with others who might find it helpful!


    Picture me as a ship captain navigating the vast ocean of programming. In this ocean, there are countless islands, each representing a different part of a program. As I set sail, I need to decide what kind of cargo each island will hold. This is where TypeScript comes into play with its variable and constant declarations.

    Each variable is a crate I load onto my ship. Before setting sail, I need to label each crate with its contents to ensure I deliver the right goods to the right island. In TypeScript, I use the let or const keyword to declare these crates. For instance, if I want to transport a number, I label the crate as let distance: number = 100;. This tells me, as the captain, that the crate contains a number, so no surprises when I open it later.

    Constants are special crates that I know won’t change their contents, like a sealed treasure chest. To declare a constant, I use const instead of let. For example, const pi: number = 3.14; is like saying, “This crate contains the value of pi, and it’s not going to change, no matter how stormy the seas get.”

    By labeling my crates with specific types, I ensure that when I reach each island, I’m prepared with the exact goods needed. It prevents mix-ups, like accidentally delivering a crate of bananas when the island needed coconuts.

    So, as I sail across the programming ocean, TypeScript’s type declarations are my compass and map, guiding me to deliver the right cargo to the right destinations. Just like a well-prepared captain, I can avoid unnecessary detours and ensure a smooth journey.


    In JavaScript, I might declare a crate without specifying its contents like this:

    let cargo = 100; // Initially, cargo is a number
    cargo = "food supplies"; // Now, cargo is a string

    Here, JavaScript is quite flexible—like an ocean current that can shift in any direction. While this flexibility allows for swift changes, it can sometimes lead to confusion, as I might forget what’s inside my crates.

    However, with TypeScript, I label my crates clearly:

    let cargo: number = 100; // Declaring cargo as a number
    // cargo = "food supplies"; // This would cause an error in TypeScript

    This ensures that I won’t accidentally replace my numbered cargo with food supplies, preventing potential mishaps at sea.

    Moreover, let’s look at constants, those sealed treasure chests that remain unchanged:

    const shipName = "The Endeavor"; // In JavaScript, this remains constant
    // shipName = "The Explorer"; // This would cause an error in both JavaScript and TypeScript

    In both JavaScript and TypeScript, constants are reliable—once they’re set, they stay the same, much like a steadfast lighthouse guiding my journey.

    Key Takeaways:

    1. TypeScript Adds Structure: It helps me label my variables (crates) with specific types, reducing the risk of unexpected changes, akin to a captain ensuring the correct cargo is delivered.
    2. Flexibility vs. Safety: JavaScript offers flexibility, allowing me to change the contents of my crates freely, while TypeScript provides safety by enforcing consistent cargo types.
    3. Constants as Anchors: Constants remain unchanged across both JavaScript and TypeScript, providing stability in my programming journey.