7

I am beginner in typescript. I have a doubt in usage of "any" type.

Using "any" is basically opting out type checking if I am right. For example

  1. var num:any = 12

  2. var num = 12

we could probably use the second one itself, what's the need for 'any'?

random_user_name
  • 25,694
  • 7
  • 76
  • 115
Chinniah Annamalai
  • 101
  • 1
  • 1
  • 8
  • Well your specifically saying it's `any`.. Otherwise it's implied it's `any`.. Would become more handy if say you was restructuring code, and you wanted to specifically mark this as handling any type. It certainly has it's uses. – Keith Jun 15 '18 at 12:31
  • 1
    Javascript is valid typescript. the second one is javascript. You can use that but the explicitness of `any` is good, too. It lets you know that the developer made an informed decision to allow this variable to be dynamic – Marie Jun 15 '18 at 12:31
  • 1
    You would almost never use `any` for a number type. – baao Jun 15 '18 at 12:31
  • While you didn't ask this directly, `any` is indeed (as you say) a bit of a "cop-out" in this case. You'd likely want `var num: number = 12;` - to explicitly indicate you intend it to be used for numbers. In that way, typescript would complain if you tried to use it in a non-numeric way.... – random_user_name Jun 15 '18 at 12:32
  • If you omit the `any` in your code, then it becomes unclear to anyone reading the code (including you, possibly, in the future) - is that a mistake? Was it intended to have a type, but got missed? What should the type be? etc... `any` should be used to indicate that the type doesn't "fit" a known type (number, string, etc), or that it's possible it may change (which isn't a good pattern) – random_user_name Jun 15 '18 at 12:35
  • I disagree @cale_b. The declaration `var num = 12` is totally clear. Even traditionally strong typed languages like java allow this now because it's so clear, and `var num: number = 12` is just too much and doesn't help – baao Jun 15 '18 at 12:38
  • @bambam - Interesting. then why use typescript at all? The entire point is for it to notify you when you've used it in a way that disagrees with the declared type... – random_user_name Jun 15 '18 at 12:38
  • `let foo = 12; foo = 'bar';` results in an error because the string is not assignable to a number type. With and without typing. @cale_b – baao Jun 15 '18 at 12:41
  • 1
    @bambam - well I'll be.... you are completely right. Thanks for the convo - I learn something new here every day! – random_user_name Jun 15 '18 at 12:43
  • @cale_b yeah, me too... I actually found that by using java 10 which allows that kind of declarations now, and was wondering why a "loosely" typed language should be more strict here... – baao Jun 15 '18 at 12:50

5 Answers5

11

While the two are equivalent in use (because any is the default type when unspecified) by explicitly specifying the type as any, you explicitly declare the intent.

Intellisense, where available, will display the type as any, allowing easier understanding how your variable is meant to be used.

Kristianmitk
  • 4,528
  • 5
  • 26
  • 46
Attersson
  • 4,755
  • 1
  • 15
  • 29
  • *"...because any is the default type when unspecified..."* That's incorrect when there's an initializer, as there is in the question. When there's an initializer, the type of the variable is [inferred](https://www.typescriptlang.org/docs/handbook/type-inference.html#handbook-content) from the result type of the initializer expression. In the OP's example, the type is `number`; [proof](https://www.typescriptlang.org/play?#code/G4QwTgBAdgrgthAvBAjAJgNwFgBQB6PCCAPQH4g). – T.J. Crowder Mar 20 '23 at 12:36
9

First of all - If we speak about Typescript, lets avoid the var key-word.

We may need to describe the type of variables that we do not know when we are writing an application. These values may come from dynamic content, e.g. from the user or a 3rd party library. In these cases, we want to opt-out of type-checking and let the values pass through compile-time checks. To do so, we label these with the any type:

Example to this:

let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean

More: Types in Typescript

Sh. Pavel
  • 1,584
  • 15
  • 28
1

Any: any type. Used when impossible to know the type. When you declare type as any, you can reassign any type of value in that variable.

var num:any = 12;
num = boolean;
num = "abcd"
Farzana Khan
  • 1,946
  • 1
  • 6
  • 9
1

Using "any" is basically opting out type checking if I am right.

Right.

For example

var num:any = 12

var num = 12

we could probably use the second one itself...

When you don't provide a type annotation but you do provide an initializer (= 12), TypeScript will infer the type of the variable based on the result type of the initializer expression. In your example, for instance, it will infer number: Playground example

You'd use any if you wanted to be able to set num to something else, like a string, later, like this — but keep reading, I wouldn't do this (playground):

let num: any = 12;
console.log(num);
num = "twelve";
console.log(num);

If you left the type annotation (: any) off, that would give you an error, because the type of num is inferred to be number (playground):

let num = 12;
console.log(num);
num = "twelve";
//^−−−− Type 'string' is not assignable to type 'number'.(2322)
console.log(num);

That said, since any opts out of typechecking, it's something you want to avoid whenever possible. In that example, if I had a really good reason for allowing the variable to be a number or a string, it would be better to give it the union type string | number (and probably change its name; playground):

let x: string | number = 12;
console.log(x);
x = "twelve";
console.log(x);

That way, if I try to assign a boolean (for instance) instead, I'll get an error.

what's the need for 'any'?

The purpose of any is, as you said, to opt out of typechecking for that variable. Here's what the any documentation says:

The any type is useful when you don’t want to write out a long type just to convince TypeScript that a particular line of code is okay.

But since the whole point of TypeScript is to provide static type checking, and any opts out of static type checking, in general any is best avoided whenever possible. It's not always possible (particularly when interfacing with untyped code outside the scope of your project), but when it's possible, it's generally best.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-2

any opts out type checking as you have said. The second description you have came up with (without any) will compile too. But it is not valid(*) when you use linting like tslint.

(*) By not valid, I meant the IDE you use will pop up an alert. But, to the bottom line; any valid Javascript code is also valid for Typescript on the grounds of compiling.

vahdet
  • 6,357
  • 9
  • 51
  • 106
  • 1
    _But it is not valid when you use linting_ That's just a matter of setting – baao Jun 15 '18 at 12:33
  • 1
    Your IDE won't complain with the correct setting in tslint.json. If it does, get another one.. :-) – baao Jun 15 '18 at 12:39