Day 45: Interfaces in Typescript

mtee

Margaret W.N

Posted on August 30, 2020

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

馃挅 馃挭 馃檯 馃毄
mtee
Margaret W.N

Posted on August 30, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 56: Semantic HTML
webdev Day 56: Semantic HTML

September 27, 2023

Day 54: Internet
html Day 54: Internet

September 25, 2023

Day 35: Typescript
javascript Day 35: Typescript

August 29, 2023