I have a class object:
groupNameData: GroupNameData = new GroupNameData();
and I have an any
object
groupNameDatas: any;
I just assigned the class object values to any
object, like
this.groupNameDatas = this.groupNameData;
It means, this.groupNameDatas
(Any) can accept any kind of data, because it's an any
object.
Now I have reversed the assignment, like
this.groupNameData = this.groupNameDatas;// any to class
It's also working like my first assignment example. Why it did not throw an error like cannot convert implicitly "any" to "GroupNameData"
?
In TypeScript, objectis the type of all non-primitive values (primitive values are undefined, null, booleans, numbers, bigints, strings). With this type, we can’t access any properties of a value.
With type Object, TypeScript complains if an object has a property whose type conflicts with the corresponding property in interface Object: // @ts-ignore: Type ' () => number' is not assignable to // type ' () => string'.
Those constructs only exist because those languages force all data and functions to be inside a class; because that restriction doesn’t exist in TypeScript, there’s no need for them. A class with only a single instance is typically just represented as a normal object in JavaScript/TypeScript.
The following code produces a type error (line A) in nominal type systems, but is legal in TypeScript’s structural type system because class A and class B have the same structure: TypeScript’s interfaces also work structurally – they don’t have to be implemented in order to “match”:
This is the expected behavior (docs). Hopefully this sample will clarify it:
let someObj = new MyClass();
// someObj will be of the "MyClass" type.
let anyObject : any;
// since anyObject is typed as any, it can hold any type:
anyObject = 1;
anyObject = "foo";
// including your class:
anyObject = someObj;
// so, if it can hold anything, it's expected that we can assign our custom classes to it:
someObj = anyObj;
But how can typescript accept to assign any object to class object?
That's the fun with the any
type. Typescript can't know if your any
-typed variable holds an instance of your object or not. It's anything, so it could be an instance of your object.
If you look at the official documentation, it clearly says that with "any" all compile time checks are ignored.
Relevant snippet from the docs:
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:
let notSure: any = 4; notSure = "maybe a string instead"; notSure = false; // okay, definitely a boolean
The any type is a powerful way to work with existing JavaScript, allowing you to gradually opt-in and opt-out of type-checking during compilation. You might expect Object to play a similar role, as it does in other languages. But variables of type Object only allow you to assign any value to them - you can’t call arbitrary methods on them, even ones that actually exist:
Should you choose to use another type e.g. number or string the compile time checks kick in and you know that its not right.
let notSure: any = 4; notSure.ifItExists(); // okay, ifItExists might exist at runtime notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check) let prettySure: Object = 4; prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.
The any type is also handy if you know some part of the type, but perhaps not all of it. For example, you may have an array but the array has a mix of different types:
let list: any[] = [1, true, "free"]; list[1] = 100;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With