김 양의 멋따라 개발따기

TypeScript - Type Alias과 interface 본문

TypeScript

TypeScript - Type Alias과 interface

개발따라김양 2022. 11. 22. 01:25
//interface
interface DogInterface {
	name : string
	age : number
}

//type Alias
type DogType= {
	name : string
	age : number
}

//object -> 둘 다 가능
const Rive: DogInterface = {
	name : "rive";
    age : 21
}

const Seio: DogType = {
	name : "seio";
    age : 12
}

//class -> 둘 다 가능
class Dog1 implements DogInterface {
	name:string;
    age:number;
	}
    
class Dog2 implements Dogtype {
	name:string;
    age:number;
	}
    
    
//Extends -> 둘 다 가능

interface OneDogInterface extends DogInterface {
	like:string
}

type TwoDogType = Dogtype & {like:string}

//결합 -> interface만 가능!!
interface DogInterface {
	like : string
}

//computed properties -> type alias만 가능!!
type Dog = {
	name: string,
    age:number
}

type Name = Dog["name"]; // string 타입

type DogAge = number;

//union 타입 -> type alias만 가능!!
type HouseDog = "rive" | "sieo" | "noah"

1.  Interface

  • 규격사항
    • 규격사항을 통해 구현할 목적이 있는 경우 자주 사용

2. Types

  • 데이터를 담을 목적으로 자주 사용

 

 

 

출처 :