myHotTake

Tag: eliminate any usage

  • How to Eliminate Unnecessary any in TypeScript Code?

    If you enjoy this story, feel free to like or share it!


    I am the captain of a ship, setting sail on a long voyage. My ship, once filled with a diverse crew, has just undergone a major upgrade. We’ve replaced some old sails and ropes with new, more efficient ones. However, as we set sail, I notice that some of my crew members are acting like “any” sailors—they’re not specialized and can be doing anything, which makes it hard to know if they’re truly needed or if they’re doing their jobs correctly.

    I start by gathering the crew on deck. I need to identify these “any” sailors who are not performing specific roles. I ask each crew member to tell me their specific duties. If someone says they are just “any” sailor, I dig deeper. I need to understand where they fit in this ship’s journey. Are they really needed on the crow’s nest, manning the sails, or steering the ship? Or are they just drifting from task to task without adding real value?

    Once I identify these “any” sailors, I begin retraining them. I give them specific roles and responsibilities, such as a navigator, a quartermaster, or a helmsman. This way, each person has a clear purpose, and my ship sails more smoothly and efficiently. Now, everyone knows what they are supposed to do, and I can trust that each task is handled by an expert.

    As we continue our voyage, I regularly check in with my crew. I ensure that no one slips back into being an “any” sailor. By doing so, my ship becomes a well-oiled machine, ready to face any storm or challenge that the sea might throw our way. If you found this analogy helpful, feel free to like or share!


    Here’s an example of what I might find:

    let cargo: any = "gold";
    cargo = 100; // Initially a string, now a number
    
    function processCargo(cargo: any): void {
      console.log("Processing", cargo);
    }
    
    processCargo(cargo);

    In this code, the use of any means I could be passing anything into processCargo, and it could lead to unexpected results. Just like my crew needed specific roles, my variables need specific types to ensure everything functions smoothly.

    To fix this, I start by examining each any usage. I question its purpose and try to replace it with a more specific type. Here’s how I might refactor the code:

    let cargo: string = "gold";
    // cargo = 100; // This would now cause a type error
    
    function processCargo(cargo: string): void {
      console.log("Processing", cargo);
    }
    
    processCargo(cargo);

    Now, I’ve assigned a specific type to cargo, ensuring consistency and preventing type-related errors. If I need to handle multiple types, I might use union types or create a more structured approach:

    type Cargo = string | number;
    
    let cargo: Cargo = "gold";
    cargo = 100;
    
    function processCargo(cargo: Cargo): void {
      if (typeof cargo === "string") {
        console.log("Processing string cargo:", cargo);
      } else {
        console.log("Processing numeric cargo:", cargo);
      }
    }
    
    processCargo(cargo);

    Key Takeaways:

    1. Identify and Examine: Just like identifying “any” sailors, scrutinize each any usage to understand its purpose and necessity.
    2. Assign Specific Types: Replace any with more specific types to enhance code readability and reliability.
    3. Use Union Types: If variables can hold multiple types, consider using union types for clarity and flexibility.
    4. Regular Maintenance: Continuously review your code to prevent unnecessary any usages from slipping back in, much like monitoring a crew.