SparkV icon indicating copy to clipboard operation
SparkV copied to clipboard

The Issue With "Any" Typings

Open Luna-devv opened this issue 1 year ago • 3 comments

What is any?

The special type any is used to tell TypeScript that a variable can be of any type. A variable with the type any can be a string, a number and or anything else! When our previous code block is updated to use any, the assignment of a string is allowed, as shown below. You can assign any type to a variable annotated with any.

Why would you not use it?

Using any eliminates the type checking provided by TypeScript, which is an important reason for using TypeScript in the first place instead of JavaScript. By using any, you expose yourself to problems that are difficult to track and debug, especially when the code is used in production.

In other words, using any for a variable type in TypeScript is similar to writing JavaScript.

What are the alternatives?

1. Using the correct type

The first (and best) is to use the right type for your variable. All too often, you use any because they are using a function from third-party libraries or external websites (e.g. Stackoverflow) and don't know what type is being returned!

To save time and not understanding what type is being used, you take a shortcut and use any for the variable type, which causes problems later when the code is updated (what if the type of the object returned from the third-party library changes? what if you try to access an attribute that is deprecated in new versions of the library?).

Instead, it is important to understand the type that is being returned and type your variable accordingly. Take the time to understand the code you are copying/pasting from somewhere else, and take the time to understand the library you are including in your code. Knowing the type you are using saves time and avoids mistakes, but ensures you understand the code and can solve the problem you are working on.

2. Using unknown

The second option is to use unknown if you do not know the type and want to ensure type safety. When using unknown, you can assign all types to a variable, but you cannot assign a variable with the unknown type to another variable with a type.

According to the Pull Request, which introduced unknown into TypeScript!

This PR adds a new top type unknown, which is the type-safe counterpart to any. Anything can be assigned to unknown, but unknown can only be assigned to itself and any, with no type assertion or control-flow-based constraint. Similarly, no operations are allowed on an unknown without first an assertion or a restriction to a more specific type.

Luna-devv avatar Sep 16 '22 20:09 Luna-devv

Thanks for making the PR. My code quality during the change from JavaScript to TypeScript I admit wasn't the greatest. I was very lazy with it, and added any everywhere. At some point in the future I will fix the typings, but for now I am focusing on my personal life since school just started for me recently and I need to focus.

KingCh1ll avatar Sep 16 '22 20:09 KingCh1ll

It is just defeating the point of using TypeScript in the first place, since you use TypeScript for the typings obviously, and I also just checked that you have // "strict": true, aka you're not using strict mode which is the 2nd out of 2 reasons to use TypeScript.

Honestly at this point I don't know why you decided to use TypeScript, if you are literally avoiding ALL POINTS that make TypeScript to what it is.

Luna-devv avatar Sep 16 '22 20:09 Luna-devv

I use TypeScript mainly because I was bored and wanted to try a new language out, especially since I've been hearing good things about it. I heard from a friend that the language made JavaScript faster, and that was just what I wanted for my Discord bot.

KingCh1ll avatar Sep 16 '22 20:09 KingCh1ll

This issue is stale for 25+ days, so it will be closed in 5 more days

KingCh1ll avatar Nov 06 '22 03:11 KingCh1ll