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)


11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields


Read the whole post now!