정의되어 있는 타입 y에 x를 할당할 수 없다는 error이다.
예시
export const searchRepositories = (term: string) => {
return async (dispatch: Dispatch<Actions>) => {
dispatch({
type: ActionType.SEARCH_REPOSITORIES,
})
try {
const {data} = await axios.get('https://registry.npmjs.org/-/v1/search',{
params: {
text: term
}
})
const packageData = data.objects.map((result: any) => {
return result.package
})
dispatch({
type: ActionType.SEARCH_REPOSITORIES_SUCCESS,
// error : Type 'number' is not assignable to type 'string[]'
payload: 133131313
})
}catch(err) {
if(err instanceof Error){
dispatch({
type:ActionType.SEARCH_REPOSITORIES_ERROR,
// // error : Type 'string[]' is not assignable to type 'string'
payload:['hi']
})
}
}
}
}
Dispatch로 정의한 타입은 다음과 같다.
interface SearchRepositoriesAction {
type: ActionType.SEARCH_REPOSITORIES
}
interface SearchRepositoriesSuccessAction {
type : ActionType.SEARCH_REPOSITORIES_SUCCESS
payload: string[]
}
interface SearchRepositoriesErrorAction {
type: ActionType.SEARCH_REPOSITORIES_ERROR
payload: string
}
export type Actions = SearchRepositoriesAction | SearchRepositoriesSuccessAction | SearchRepositoriesErrorAction