Introduction to TypeScript Cast Object
The typescript cast object is one of the features for converting one type of variable value. Data types may be integer, string, float, or double values, or it may be some interfaces to convert it. If the user has written some functions based on the default classes, we can call the methods for the specific classes by using the object reference. So we are unable to use the object reference in all the sections based on their needs. we used a cast object to convert the object into another type with some references.
Syntax of TypeScript Cast Object
Typescript is one of the web-based languages before JavaScript. we used typescript, is mainly referred to the type of conversions that are for to convert one data type to the other data type. Here we used assertions to enable override the default behaviors, and also type assertions are fully compiled time constructs, and it’s one of the ways to analyze the codes.
Code:
class classname{
constructor()
{
--some logics—
{
function()
{
}
window.onload=() = >
{
variablename = new classname();
variablename.function();
};
The above code is the basic syntax for traversing the downcasting in the application. Here we used some class names to construct the applications, and also we can create the instance for calling the class name so it will raise it from upward casting to downward casting. As we used n number of type casting will be applicable for this script, we can convert the object type to interface type, etc these are some frequency type casting performed by the user end.
How Cast Object Work in TypeScript?
The Typescript generally converts one data type to another data type by using the cast operation. That cast object is one of the features for converting the object type of values to another type. We can also cast some generic types of objects to the custom object types by using the <> symbol, or it’s a keyword for converting the object to another type like an array, etc. Generally, the object has, and already declared, the multiple properties and their attributes by using some methodology like key-value pairs. When we use an object in the class, the variable has the array datatype then we can store and retrieve the datas by using the keys like object.keys(), object.values(). By using the map() method, we can define the values incallbackconcept for storing and retrieving the datas from the backend. If we want to cast the object to string data types by using toString() method, we can change the object type to a string data type. Mainly we used class objects with the help of constructors it’s creating, and they will be defined with properties, constructors, and pre-defined methods.
Examples of TypeScript Cast Object
Following are the examples are given below:
Example #1
Code:
const eg = {
'demo': {type: 'string'},
'demo1': {type: 'string'},
'demo2': {type: 'string'},
'demo3': {type: 'string'},
'demo4': {type: 'string'},
'demo5':{type:'string'},
'demo6':{type:'string'}
};
let vars = [];
for(let vars1 in eg) {
vars.push({[vars1] : eg[vars1]});
}
console.log("Welcome To My Domain your first object variable is:", eg);
console.log("Thanks user after converting object to arrays your variable is:", vars);
function erg(labeledObj: { value: string }) {
console.log(labeledObj.value);
}
let vars2 = { key: 'demo', value: "Thanks users have a nice day" };
erg(vars2);
Output:
In the above example, we can get the object type to array types here we can use functions with default keywords and variables with data types. By using for loop, we can iterate the values and store them on a separate variable, like letting it is a local storage variable it does not use on the global type.
Example #2
Code:
var example = {
siva: { first:6245, second:1725},
raman: { first:6245, second:1725,five:'qjwkgje jweg wqegj wjeg kwjeg wejkg wejkg kwhe kwhej'},
sivaraman: { first:6245, second:1725,four:'jges jwge wjge wjeg kwjegr jkwegr jkewgr'},
kweg: { first:6245, second:1725},
ksdhjg: { first:6245, second:1725,four:'jgsd jags jsd g wjeg qwkjeg wekjg kqweg kqwge'},
kjlsldj: { first:6245, third:827,four:'jsgd34 asjkdgf jgds wjhg jweg kwjge kweg kwejg'},
}
let vars = [];
Object.keys(example).map(function(key){
vars.push({[key]:example[key]})
return vars;
});
console.log('Welcome To My Domian Object of the people are=',example)
console.log('After converting the people object to array is=',vars)
Output:
In the second example, we used object casting by using the keys() method. We can declare the variable with a key-value type. The key must be a unique one, and the value may be any type. Using Object.keys() method, we can iterate the value and traverse the array elements in upcastingi.e) upcasting to downcasting. Finally, it will return the array as the return type.
Example #3
Code:
class demo {
sample:string;
constructor(sample1:string) {
this.sample = sample1
}
views():void {
console.log("Welcome To My Domain:"+this.sample)
}
}
class demo1 extends demo{
example()
{
console.log("your third example this thanks")
}
}
var vars2 = new demo("Sivaraman")
console.log("Have a Nice Day user: "+vars2.sample)
vars2.views();
function testss(d:demo) {
if (d instanceof demo1) {
d.example();
}
}
Output:
In the final example, we used class with methods. We can implement parent-child relationships. That is, we have to extend the parent class to child class, and we have created the instance or object of the parent class var reference= new example(); this object reference is meant by the downcasting the code snippets and traverses from upward to downward casting by using the console.log() statement we can print the output on the console screen.
Conclusion
In the concluded part, typescript is the superscript of javascript. It will be more or less equal to the javascript, but it will be executed at the compile time. Typescript casting is the most important concept. When we use javascript, we are unable to cast the variables, but in typescript, we can cast it from object to class, object to the interface, object to strings, etc, based on the user requirement, we can cast it.
Recommended Articles
We hope that this EDUCBA information on “TypeScript Cast Object” was beneficial to you. You can view EDUCBA’s recommended articles for more information.