TypeScript: Types
A simple guide to “interface” data type in TypeScript
In this article, we are going to learn about the interface
type to enforce restrictions on the shape of objects.
An interface is a shape of an object. A standard JavaScript object is a map of key:value
pairs. JavaScript object keys in almost all the cases are strings and their values are any supported JavaScript values (primitive or abstract).
An interface tells the TypeScript compiler about property names an object can have and their corresponding value types. Therefore, interface is a type and is an abstract type since it is composed of primitive types.
When we define an object with properties (keys) and values, TypeScript creates an implicit interface by looking at the property names and data type of their values in the object. This happens because of the type inference.
In the above example, we have created an object student
with firstName
, lastName
, age
and getSalary
fields and assigned some initial values. Using this information, TypeScript creates an implicit interface type…