티스토리 뷰

웹 개발/Typescript

type vs. interface

투자유v 2022. 1. 4. 11:15
728x90
// 5. type alias vs. interface
// 5-1. function
interface IFunction {
    (name: string, age?: number): void;
}

type tFunction = (name: string, age?: number) => void;

// 5-2. array
type tArray = string[];
interface IArray {
    [index: number]: string;
}

// 5-3. intersection
interface ErrorHandling {
    success: boolean;
    error?: { message: string };
}

interface ArtistData {
    artists: { name: string }[];
}

type ArtistsResponseType = ArtistData & ErrorHandling;

interface IArtistsResponse extends ArtistData, ErrorHandling {};

let tar: ArtistsResponseType;
let iar: IArtistsResponse;

// 5-4. union type (only type)
type stringOrNumber = string | number;

// 5-5. declaration merge(only interface)
interface DeclarationMerge {
    name: string;
}
interface DeclarationMerge {
    age?: number;
}

둘 다 타입을 정의하는 방법인데, interface는 타입을 새로 만드는 것 같고, type은 이미 있는 타입들을 조합해서 이름을 정하는 것 같다.

'웹 개발 > Typescript' 카테고리의 다른 글

class 기본 (readonly, index signatures)  (0) 2022.01.04
class 기본 (초기화, getter, setter)  (0) 2022.01.04
interface 기본  (0) 2022.01.04
function in interface  (0) 2022.01.04
optional property in interface  (0) 2022.01.04
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함