How to make a class implement methods and properties using an interface in TypeScript?
Tags: typescript
Published: 2022-04-24 00:00:00

---
  title: How to make a class implement methods and properties using an interface in TypeScript?
  published: true
  tags: TypeScript
  date: Sun Apr 24 2022 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/make-class-implement-methods-properties-using-interface-typescript
  cover_image: https://melvingeorge.me/_next/static/images/main-d11d0991aa3ce5ed1037f50f40bc9524.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/make-class-implement-methods-properties-using-interface-typescript)
  
To make or force a [`class`](https://melvingeorge.me/blog/create-class-typescript) to implement certain methods or properties, we can use an `interface` to make it implement the required methods and properties.

To do that we can start by writing the class name followed by writing the `implements` keyword followed by the name of the `interface` we need to use and then the class body.

### TL;DR

```ts
// a simple car structure interface
interface CarStructure {
  name: string;
  yearMade: number;
  startEngine: () => void;
}

// a class that satisfies the `CarStructure` interface
// using the `implements` keyword
class Car implements CarStructure {
  name: string;
  yearMade: number;

  constructor(name: string, yearMade: number) {
    this.name = name;
    this.yearMade = yearMade;
  }

  startEngine() {
    console.log("Car started...");
  }
}
```

For example, let's first create a simple `interface` with 2 properties with its type and one method signature.

It can be done like this,

```ts
// a simple car structure interface
interface CarStructure {
  name: string;
  yearMade: number;
  startEngine: () => void;
}
```

As you can see from the above code we have defined 2 properties called `name` and `yearMade` with `string` and `number` respectively. We have also defined a `startEngine` method signature that doesn't need to have any parameters as well as doesn't return a value.

Now we can create a `class` and then make the class implement these properties and methods using the `CarStructure` interface using the `implements` keyword.

It can be done like this,

```ts
// a simple car structure interface
interface CarStructure {
  name: string;
  yearMade: number;
  startEngine: () => void;
}

// a class that satisfies the `CarStructure` interface
// using the `implements` keyword
class Car implements CarStructure {}
```

Now, as soon as you write the `implements CarStructure` the TypeScript compiler will throw an error saying `Type 'Car' is missing the following properties from type 'CarStructure': name, yearMade, startEngine`. In simple words, it is saying that we need to make the properties as well as the method that is in the `CarStructure` interface. This helps us implement the methods and the properties in the class with the correct types.

So let's implement the `name`, `age`, and `startEngine` properties and methods.

It can be done like this,

```ts
// a simple car structure interface
interface CarStructure {
  name: string;
  yearMade: number;
  startEngine: () => void;
}

// a class that satisfies the `CarStructure` interface
// using the `implements` keyword
class Car implements CarStructure {
  name: string;
  yearMade: number;

  constructor(name: string, yearMade: number) {
    this.name = name;
    this.yearMade = yearMade;
  }

  startEngine() {
    console.log("Car started...");
  }
}
```

As soon as we implement the properties and methods in the `Car` class, the errors are gone and the `CarStructure` interface structure is satisfied.

We have successfully made a class implement methods and properties using an interface in TypeScript. Yay 🥳!

See the above code live in [codesandbox](https://codesandbox.io/s/make-class-implement-methods-properties-using-interface-typescript-tpc9qq?file=/src/index.ts).

That's all 😃!

### Feel free to share if you found this useful 😃.

---