본문 바로가기
TypeScript

제네릭 Generic

by 찬찬2 2024. 6. 5.
// Use case
type ApiResponse<Data> = {
    data: Data
    isErrpr: boolean
}

// case#1
const response: ApiResonse<{ status: number }> = {
    data: {
        status: 200
    },
    isError: false
}
const response: ApiResonse<string> = {
    data: "abcde",
    isError: false
}

// case#2 = 타입의 확장
type UserResponse = ApiResponse<{ name: string, age: number }>;
type BlogResponse = ApiResponse<{ title: string }>;

const response: UserResponse = {
    data: {
        name: 'abcde',
        age: 1
    },
    isError: false
}

const response: BlogResponse = {
    data: {
        title: 'abcde'
    },
    isError: false
}

댓글