type MockRepository<T = any> = Partial<Record<keyof Repository<T>, jest.Mock>>;
위와 같은 코드를 보고 정신이 혼미해져서 타입 관련 정리를 해본다.
interface Todo {
title: string;
description: string;
}
function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
return { ...todo, ...fieldsToUpdate };
}
const todo1 = {
title: "organize desk",
description: "clear clutter",
};
//todo1의 경우 title과 description이 모두 있어야 하지만
//Partial<Todo>의 경우 타입이 optional하기 때문에 title이 없어도 된다
const todo2 = updateTodo(todo1, {
description: "throw out trash",
});
interface PageInfo {
title: string;
}
type Page = "home" | "about" | "contact";
//home, about contact에 각각 pageinfo타입이 매핑된다.
const nav: Record<Page, PageInfo> = {
about: { title: "about" },
contact: { title: "contact" },
home: { title: "home" },
};
type MockRepository<T = any> = Partial<Record<keyof Repository<T>, jest.Mock>>;