Object in TypeScript

The object is a set of variables or functions or array or scalar value, Which we can use as a normal or also can be able to pass through a function.

to declare an object first we have to take a variable using the keyword var or let and then have to provide the variable name and then after the equal sign has to be given surrounded by double curly brackets, some variable or key and its value separated by a colon and can provide a function after providing colone of a key or also can declare a set of values or array after providing colon.

var sample_object_name = { 
   var1: "value1", 
   var2: "value",  
   var3: function() {
   var4:['val1', 'val2']

Object as a function parameter:

To pass the values of an object through a function first declare an object with some key and its value separated by the key and value by colone and provide a coma after each key and its value if it’s not the last one. Then declare a function and inside the functions, the first bracket gives an object name and then the variables with their types separated by a colon, and each variable and its type will be separated by a comma.
Now inside the function, you can be able to use the variable.
Then call the function to see the result. See the below example of passing an object through a function.

var student = { 
var displey_student = function(student_obj: { firstname:string, lastname :string }) { 
   console.log("Students name is : " + student_obj.firstname + ' ' + student_obj.lastname) 

Anonymous Object

An anonymous object is you can pass direct an abject while calling a function surrounded by a curly bracket.

var displey_employee = function(emp_obj: { firstname:string, lastname :string }) { 
   console.log("Employee name is : " + emp_obj.firstname + ' ' + emp_obj.lastname) 
displey_employee({firstname:"Abcd", lastname:"Efgh"});

Duck Typing TypeScript:

The duck type is a concept of checking whether some objects have the same properties,
In duck-typing, more than one object is considered if both share the same properties to be the same type.

In the below example we are going to see the establishment of the quote:

“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”
-by James Whitecomb Riley

That is while we are passing an object through a function typescript does not know if the central datatype is sending or not that is it will check like the walks, swims, or quacks but will not check whether the burd is actually a duck or not it will check only its property so to prevent that we have to make the data type of object in such a way that will satisfy the duck by its property.

interface Cordinate { 
function sampleAdditionCordinate(point1:Cordinate,point2:Cordinate):Cordinate { 
   var a = point1.a + point2.a 
   var b = point1.b + point2.b 
   return {a:a,b:b} 

var anotherCordinate = sampleAdditionCordinate({a:3,b:4},{a:5,b:1})  


if we want to spend some time on one value instead of two as an object through the function, typescript will not support that.

Share The Post On -