Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need the any type in Typescript? [duplicate]

I'm starting to learn Typescript. And I'm wondering why do we need any type if every variable is a sort of "any", I assume if you don't specify the type.

For example, these codes output would be always yes for whatever value of x and y:

var x = "d";
var y: any = 5 ;
alert(y);
y = x ;
alert(y);
if (x===y) 
  alert("yes");
else
  alert("no");
like image 255
Filip Machaj Avatar asked Oct 16 '25 14:10

Filip Machaj


2 Answers

The any type is an escape hatch—it allows you to effectively turn off Typescript’s type checking. Sometimes, there just isn’t a good way to tell Typescript what you are doing, so you use any to work around that.

And types are not necessarily any when you leave off a type annotation—in many cases, Typescript will infer a type, and then enforce that. let x = 2; will infer x as having type number—and using x = "cat"; later will create an error. In such cases, you must use any if that’s what you want.

There are other cases where you have to use any explicitly, and could not just “leave it out,” for example, with generic types:

type Pair<A, B> = [A, B];

declare function needsSomethingPairedWithString(value: Pair<any, string>): void;

For this needsSomethingPairedWithString function, we don’t care what the first element of the pair is—maybe we’ll test its type, maybe we won’t even use it, whatever, but the point is we don’t care. But we want to specify that the second element of the Pair is string. So we need to write Pair<any, string> to indicate that.

In many cases it is preferable to use unknown to any for these kinds of purposes—unknown’s meaning is closer to “don’t know and don’t care” than any’s is. However, particularly when dealing with constraints, any can be the correct type.

Finally, note the existence of the --noImplicitAny compiler flag: this will cause cases where any is inferred for a type to become errors. That forces you to either fix things so they are inferred correctly, annotate the correct type, or at the very least, explicitly annotate the any type. This is highly recommended for projects that start from the beginning using Typescript—implicit any should be reserved for projects gradually converting Javascript to Typescript, as it is a potential source of many bugs. Using --noImplicitAny forces you to be explicit about any, which means it will be immediately obvious to any developers reading it later that any is being used. This is important because any means that things are not going to be as safe as you’d like.

like image 123
KRyan Avatar answered Oct 18 '25 08:10

KRyan


if every variable is a sort of "any", I assume if you don't specify the type

This is true, but if you run TypeScript with the flag --noImplicitAny then TypeScript will no longer assume variables should have the type any in most situations, e.g.

function foo(x) {
    // x is of type any
}

The above will generate an error in --noImplicitAny mode. And so if in this specific situation you wanted x to be any, you'd have to request it explicitly:

function foo(x: any) {
    // x is of type any
}
like image 21
Daniel Earwicker Avatar answered Oct 18 '25 07:10

Daniel Earwicker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!