To understand the motivation of any
, it is necessary, rather, to understand one of the fundamental principles of Typescript, which is to enable gradual adoption.
For a long time, the Javascript ecosystem had no efficient means of checking, statically, the scripts and "programs" that were developed with this language. As the dynamic typing of Javascript, allied to the various quirks of the language, were (and still are) the source of many bugs, Typescript was created to ensure greater robustness to development. Some say, quite emphatically, that static types help a lot in this case - and although that’s not the case, I agree.
Naturally, cases would arise in which a codebase, originally implemented in Javascript, would have its developers choose to migrate it to Typescript. The poblema is that, depending on the size of the code base, the task is often difficult. Hence the need for mechanisms to ensure a gradual adoption of Typescript more flexible.
The any
is one such mechanism. It is a means of giving pliability to the programmer. The use of any
it is as if the programmer speaks to the compiler the following:
I want you, compiler, do not worry about this value. To it I assign this joker type, any
, that basically allows me to do anything with it without you filling me with patience.
That’s it.
Think about the any
as the union of all types of Typescript. A type value any
accepts anything and therefore is nothing safe. By using any
, You give up, for convenience, all the security that Typescript gives you.
And just to emphasize the definition a little more, let’s look at the typescript documentation, what it says:
Typescript also has a special type, any
, that you can use whenever nay want a specific value to cause errors of type-check.
When a value is of the kind any
, you can access any of its properties (which will also be of the type any
), call it as a function, assign it to values of any kind - in short: anything that is not syntactically illegal.
Reiterating, see that the use of any
gives up the security that Typescript offers. Therefore, in more recent versions of Typescript, the unknown
, that does something similar, but with a little more security.
Still on the any
, documentation says more:
When you do not explicitly specify a type and Typescript can’t make it out by context, the compiler will use any
as default. Assuming the option noImplicitAny
is disabled (which is the default behavior, although less secure).
With this, we can allude to the question code example:
let var1: any = "Hello World";
let var2 = "Hello World";
They are different things. In the above case, var1
will have the type any
. Already var2
, due to the possibility of Typescript inference, you will have the type string
. So it’s the same as:
let var1: any = "Hello World";
let var2: string = "Hello World";
This is one of the cases where Typescript can safely infer the type of the variable. And this is obvious to the compiler, so much so that no warning is issued or any
is assumed by default. You are assigning to var2
a string, which is in the code itself. In this type of case, it is clear to Typescript that that variable at all times will be a string (see that it will always be assigned a literal string). Therefore, it is said that string
is the guy inferred.
See the difference between this passage, in which there is no possible inference and any
is assumed; and this, with the option noImplicitAny
and error has been issued.
This is one of the powers of Typescript, which has relatively good inference ability. As we have seen before, when inference is not possible, by default, any
will be used, but there is an option that prohibits this behavior, requiring the programmer, in this type of case, to clarify the type. It is about noImplicitAny
.
So you have to be careful to know when the guy is any
and when the guy is inferred. If you are using a supported editor (like Vscode), when you hover over a value, you can see the type it takes. Then it’s easy to know.
In summary, the any
is a joker - but you have to be careful not to confuse this "joker" with the inference.
The inference, when allied to the option noImplicitAny
, is something that ensures security and robustness to the code. It’s a compiler convenience so you don’t always have, where it’s obvious by context, to spell out the type manually.
The any
is a escape-Hatch which allows you to give up a certain security for flexibility. It is a convenience that is worthwhile on occasions such as gradual migration. When Typescript is being used from day zero, it rarely makes sense, since it gives up security.
I believe it is to interface with Javascript code vanilla, which can send or receive values mixing types.
– epx
And I asked myself that same question about
unknown
....– Cmte Cardeal
@Cmtecardeal, on the
unknown
: How type "Unknown" works in Typescript?.– Luiz Felipe