DEV Community

Margaret W.N
Margaret W.N

Posted on

Day 45: Interfaces in Typescript

I wrote or rather ctrl + c and ctrl + v and Interface today. Which sparked my interest on understanding interfaces in typescript.

What are Interfaces?

An interface defines the form in which an object takes. Take an example of a work contract, it defines the code of conduct. Having signed the contract you have to adhere to the code of conduct. Interfaces are similar to this you define the options/ properties of an object, so anytime you use this interface you have to adhere to the defined form.

How do i create an interface?

interface person {
  firstName: string;
  isSleepy: boolean;
}
Enter fullscreen mode Exit fullscreen mode

How do i use an interface?

You pass in a parameter with the type of our declared interface me:person. The parameter will contain the properties of your interface.

const me:person = {
  firstName: Maggie;
  isSleepy: true;
}
//For a function
function myState(me: person){
  firstName: Maggie;
  isSleepy: true;
}
Enter fullscreen mode Exit fullscreen mode

I really enjoyed Harry Wolf's video content:

I found interfaces really similar to classes, which gets me wondering, what is the difference between the two?

Day 45

Top comments (0)