TS-Redux (2)

nyongho·2021년 9월 27일
0

받아올 API는 다음과 같이 구성되어 있다.

abilities: [
{
ability: {
name: "limber",
url: "https://pokeapi.co/api/v2/ability/7/"
},
is_hidden: false,
slot: 1
},
{
ability: {
name: "imposter",
url: "https://pokeapi.co/api/v2/ability/150/"
},
is_hidden: true,
slot: 3
}
]

타입스크립트에서는 이에 대한 타입 선언을 먼저 해준다.

export type PokemonAbility = {
        ability: {
        name: string,
        url: string
        },
        is_hidden: boolean,
        slot: number
}

그리고 dispatch에 성공했을 때 보내줄 액션에 대한 타입을 선언한다.
abilities에 위에서 선언한 PokemonAbility를 정의해주면 보다 가독성 좋게 선언이 가능하다.

export interface pokemonSuccessDispatch {
    type: typeof POKEMON_SUCCESS
    payload: {
      // PokemonAbility의 배열 모습이다 라고 선언하는 것
        abilities: PokemonAbility[]
    }
}
profile
두 줄 소개

0개의 댓글