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
var num:any = 12
var num = 12
we could probably use the second one itself, what's the need for 'any'?
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
var num:any = 12
var num = 12
we could probably use the second one itself, what's the need for 'any'?
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.
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
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"
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.
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.