Wassup TypeScript Interfaces!

Wassup TypeScript Interfaces!

·

2 min read

TypeScript is a popular superset of JavaScript that adds optional static typing to the language. One of the key features of TypeScript is its support for interfaces, which allow you to define a contract for the shape of an object. This can be useful for defining the structure of objects that will be passed as function arguments, or for creating custom types that are used throughout your application.


An interface in TypeScript is defined using the interface keyword, followed by the name of the interface. The interface definition includes a list of properties, along with their types. For example, the following interface defines a simple object with two properties, name and age, both of which are strings:

interface Person {
  name: string;
  age: string;
}t

Once an interface has been defined, it can be used to create variables and objects that conform to that specific shape. You can use the : operator to tell TypeScript that a variable or object should conform to a specific interface. For example:

let person: Person = { name: "John Doe", age: "35" };

You can also use interfaces to define function types. This can be useful when a function takes an object as an argument, and you want to ensure that the object conforms to a specific shape. For example:

interface GreetingFunction {
  (person: Person): string;
}

let greet: GreetingFunction = (person: Person) => {
  return "Hello, " + person.name;
};

Interfaces can also be extended to create new interfaces that inherit the properties of existing interfaces. This can be useful for creating more specific interfaces that add additional properties or constraints to the base interface. For example:

interface Employee extends Person {
  salary: number;
}

let employee: Employee = { name: "Jane Doe", age: "28", salary: 50000 };

In this example, the Employee interface inherits all of the properties of the Person interface, but also includes an additional property, salary.

In summary, interfaces in TypeScript are a powerful tool for defining the structure of objects and functions in your application. They allow you to create custom types that can be used throughout your code, and provide a way to ensure that objects conform to a specific shape. This can help to catch errors early on, reducing the chances of bugs and making your code more maintainable.


Thanks in advance for reading this article…🚀

I am more than happy to connect with you on

You can also find me on