본문 바로가기

Frontend/TypeScript

Interface

반응형

Interface is a class that defines attributes without initial values. It makes our life so much easier when we work with TypeScript as we can define a type once and use it across out application.

How to Use It?

Let's see how we can make the code below simpler using the interface.

let object: { name: string; age: number; married?: boolean } = {
  name: 'tom',
  age: 20,
}

Create a interface of the object above with specified attributes. We can alose define a method.

interface Person {
  name: string
  age: number
  married?: boolean
  walk?: () => void // method
}

Now, use the interface as a type.

let person1: Person = {
  name: 'tom',
  age: 2,
}

It looks a lot cleaner, right?

Source Code

https://github.com/jin-co/web-mobile/tree/master/TypeScript

 

GitHub - jin-co/web-mobile

Contribute to jin-co/web-mobile development by creating an account on GitHub.

github.com

 

728x90
반응형

'Frontend > TypeScript' 카테고리의 다른 글

Generic  (9) 2023.05.12
Decorator  (3) 2023.05.12
TypeScript  (0) 2023.03.06