TypeScript's Wildcard

TypeScript's Wildcard

The Ins and Outs of the 'any' Keyword

Cover photo credit: turing.com

Introduction

The reason TS is so popular is because of the strict data type checking it brings to our code, but sometimes for reasons best known to you, you might want to avoid any strict checking(pun to be discovered).

Read on to find out how.

Any?

Typescript(TS) is a complex tool. A complex tool should come with complex behavior. An example of such behavior is any. any is the default data type TS assigns to any variable in which it cannot infer its data type.

Here:

let mysteryBox;
function getGift() {
    return true
};

// After hundreds of lines of code and modules...
mysteryBox = getGift();
console.log(mysteryBox);
  • If you hover over the last line of code(LOC), the data type of mysteryBox appears as any.

  • Take, for instance, Andy a developer who was expecting the mysteryBox variable to return a gift string, because his teammate, Bandy told him, the code was ready for production.

  • Now, when Andy takes the code live, it would run into an error that might lead to other errors and break the codebase depending on the design pattern.

  • Allowing this type of behavior in our code can undermine the reason we use TS in the first place. It becomes easy to assign types wrongly, and bypass type safety. Hence, we should avoid using any data type as often as possible.

Remember in the last article, we said there are specific situations where specifying data types is good practice? This is one of those situations. If Bandy, had specified the mystery box as a string data type, this would have saved Andy days of debugging because of TS' static checking.

let mysteryBox : string; // This triggers static checking
function getGift() {
    return true
};

// After hundreds of lines of code and modules...
mysteryBox = getGift();
console.log(mysteryBox);

Why Any?

Any is a special type to specify on an entity(variable, function, etc) when you don't want the entity to cause a type-checking error. When you assign Any to an entity, any operation on it becomes legal to TS. If you do not assign a data type to an entity, TS also can infer it as Any data type.

To stop this, TS has a special property called noImplicitAny which we can turn on in the TS configuration file(Something we would look at in future articles).

Even though there might be some cases, you don't want to use any Any type in your TS codebase, horrible things could happen.

Conclusion

TypeScript is an amazing tool that helps us catch bugs in our code before they even happen.

But sometimes, we just want to be rebels and avoid its strict rules, you know what I mean? That's where the any keyword comes in. It's like a wildcard that lets us get away with anything without getting caught! But be warned, it can be risky and cause problems down the line. So, while it's okay to use any sometimes, it's best to use specific data types as much as possible to keep your code safe and sound.

Don't forget, TypeScript is your friend, so play by the rules most of the time!