Day 45: Interfaces in Typescript
Margaret W.N
Posted on August 30, 2020
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;
}
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;
}
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
Posted on August 30, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.