Updated April 6, 2023
Introduction to TypeScript Object Type
TypeScript object type is type of any non-primitive values. TypeScript has introduced new type called as object with version 2.2. It represents all non-primitive types. There are some Primitive types such as string, number, bigint, null, boolean, symbol, undefined. All other types are considered to be non-primitive types. With this object type, use can not access any properties of value. Basically, in JavaScript, data is grouped and passed data through objects. In similar way, in TypeScript, this concept is represented through object types. Here we will see the syntax of declaration and how is it used for implementing logics.
Syntax:
Given below is the syntax of how TypeScript object type is declared:
let <object_name> : object;
object_name = {
param1: value1,
param2: value2,
param3: value3,
param4: value4
};
Here, we are declaring an object type in TypeScript and passing some parameters to it. These object types can be anonymous or can be even used by interfaces or type alias.
Examples of TypeScript Object Type
Given below are the examples mentioned:
Example #1
Declaration.
Code:
let student: object;
student = {
name: 'Karthik',
rollNo: 25,
age: 12,
standard: '6B'
};
console.log('Here are the details of Student with Roll. 25', student);
Output:
So here we have declared it, as student and passed 4 parameters to the student object, thereby printing the values on console. Here, if user tries to assign any primitive value directly to object student, we will get an error as ‘Type “X” is not assignable to type student’.
Basically, TypeScript objects such as student in the above example has fixed list of properties. And hence if user tries to access property that does not exist with object student, we will get an error:
‘Property “X” does not exist on type ‘student’.
Let us see the above two errors practically.
Example #2
TypeScript object type: basic errors.
Code:
let student: object;
student = {
name: 'Karthik',
rollNo: 25,
age: 12,
standard: '6B'
};
student='26';
console.log('Here are the details of Student', student);
console.log('Here are the details of Student', student.schoolName);
Output:
So here the first error, we had assigned a string value to the object and hence not assignable error.
The second error is as schoolName parameter does not exist with object type student and hence does not exist error.
In TypeScript, user can be able to specify properties of the object type. It should be first declared with type of each parameter passed to the object.
Syntax:
let student: {
name: string;
rollNo: number;
age: number;
standard: string;
schoolName: string;
};
Example #3
With specified parameters.
Code:
let student: {
name: string;
rollNo: number;
age: number;
standard: string;
} = {
name: 'Karthik',
rollNo: 25,
age: 12,
standard: '6B'
};
console.log('Here are the details of Student', student);
Output:
There is no change in the output based compared to Example 1, but there is a difference in implementation as here we have specifically declared the parameters.
TypeScript also has property modifiers, i.e., each property of an object can specify whether the property can be optional. Most of the time, users find it difficult to deal with objects with property sets. In such cases, user can mark them as optional by adding question mark at the end of the property name like for e.g., name? : string;
Interface is also a way to describe the property type of a TypeScript object. Declaring object is fine but the type of each property is not defined in Example 1. If user wants to use this object with any function and forgets to pass on all the parameters, there might be a chance of getting garbage value and hence to prevent garbage values, TypeScript uses an interface for object types.
Example #4
With Interface.
Code:
interface Employee {
name: string,
empID: number,
designation: string,
company: string,
yearsExp: number
}
let empInfo = (emp: Employee):string => {
return `Employee ${emp.name} with ${emp.empID}
has been promoted as ${emp.designation} in ${emp.company} based on her experience of ${emp.yearsExp} years.`
}
let emp = {
name: "Riya",
empID: 7632,
designation: "TA",
company: "Virtusa",
}
console.log(empInfo(emp))
Output:
So based on the Interface theory, here one of the parameter is not assigned to any value i.e. yearsExp and hence it is undefined in the output.
Below is the error:
Conclusion
We have seen the syntax of it, how it is declared and how is it used in programming. We have also seen another syntax where type of the parameter is provided so that there will be no confusion in assigning values to parameters. We have also seen few examples which will help to understand concept in a better way, also seen what kind of errors user would face while programming.
Recommended Articles
We hope that this EDUCBA information on “TypeScript Object Type” was beneficial to you. You can view EDUCBA’s recommended articles for more information.