3

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");
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • 3
    See the [documentation](http://www.typescriptlang.org/docs/handbook/basic-types.html#any). The `any` type allows you to *opt out* of type checking for parts of your code that cannot easily be made type safe. It's best to use it sparingly, if at all. I'm almost positive there's a canonical Stack Overflow question/answer for this; searching... – jcalz Dec 04 '19 at 20:17
  • 1
    Try changing that to `x = y;` The compiler should warn you that you are trying to assign a number to a string (or something to a string). – Heretic Monkey Dec 04 '19 at 20:18
  • "*if every variable is a sort of "any"*" - you've answered your own question there. `any` is precisely the type that we need a variable to have if it needs to hold any value. – Bergi Dec 04 '19 at 20:18
  • "For example, these codes output would be always yes for whatever value of x and y" Well yeah, because they have the same value. Why wouldn't you expect them to be equal? For the record, all the type information is lost when your code is transpiled to JavaScript anyway - it has no idea what "any" is. The type information is only there to make it easier for you as a developer. – John Montgomery Dec 04 '19 at 20:20
  • 1
    "*these codes output would be always yes for whatever value of x and y*" - except for `NaN` :-P – Bergi Dec 04 '19 at 20:21
  • Possible duplicate of [Type intersections using any](https://stackoverflow.com/a/46673732/2887218) – jcalz Dec 04 '19 at 20:22
  • It's unclear what the question is. As HereticMonkey explained, `x` is not of type `any`. Also note that many compile with `--noImplicitAny` to prevent any being inferred forcing you to explicitly say `any` in the rare cases you may need it. In your case, you need to type `y` as `any`, otherwise it would be inferred as a number and the compiler would not allow you to assign `x` (an inferred string) to it – Ruan Mendes Dec 04 '19 at 20:31

2 Answers2

2

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.

KRyan
  • 7,308
  • 2
  • 40
  • 68
1

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
}
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
  • 1
    I mentioned something similar in a comment, wasn't sure if this was what the OP was asking about. But the OP does seem to be misunderstanding inferred types and understanding `--noImplicitAny` is likely what will help them clear it up. – Ruan Mendes Dec 04 '19 at 20:32