typescript Alias

jangdu·2022년 10월 28일
0

typescript

목록 보기
4/16

Type Alias

특정타입에 별명을 붙이는 용도로 사용, 객체를 위한 타입이나 배열 등 어떤 타입도 별명을 지을 수 있음

type Person = {
    name: String;
    age?: number;
};

// &: Intersection으로 두개이상의 타입을 합침
type Developer = Person & {
    skills: string[];
}

const person: Person = {
    name: 'dudu'
};

const expert: Developer = {
    name: 'duho',
    skills: ['js']
};

// Person[]은 People이란 타입으로 사용 가능
type People = Person[];
const People: People = [person, expert];

type Color = 'red' | 'black';
const color: Color = 'red';
const colors: Color[] = ['red', 'black'];

typescript의 type과 interface는 거의 차이가 없어 일관성있게만 사용하자

profile
대충적음 전부 나만 볼래

0개의 댓글