작성
·
693
1
제가 강의를 따라하면서 코드를 아래처럼 작성했습니다
interface PhoneNumberDictionary {
[home: string]: {
num: number;
};
}
interface Contact {
name: string;
address: string;
phones: PhoneNumberDictionary;
}
// api
// TODO: 아래 함수의 반환 타입을 지정해보세요.
function fetchContacts(): Promise<Contact[]> {
// TODO: 아래 변수의 타입을 지정해보세요.
const contacts = [
{
name: "Tony",
address: "Malibu",
phones: {
home: {
num: 11122223333,
},
office: {
num: 44455556666,
},
},
},
{
name: "Banner",
address: "New York",
phones: {
home: {
num: 77788889999,
},
},
},
{
name: "마동석",
address: "서울시 강남구",
phones: {
home: {
num: 213423452,
},
studio: {
num: 314882045,
},
},
},
];
return new Promise((resolve) => {
setTimeout(() => resolve(contacts), 2000);
});
}
setTimeout(() => resolve(contacts), 2000);
여기서 contacts에서 에러가 나는데
Argument of type '({ name: string; address: string; phones: { home: { num: number; }; office: { num: number; }; studio?: undefined; }; } | { name: string; address: string; phones: { home: { num: number; }; office?: undefined; studio?: undefined; }; } | { ...; })[]' is not assignable to parameter of type 'Contact[] | PromiseLike<Contact[]>'.
Type '({ name: string; address: string; phones: { home: { num: number; }; office: { num: number; }; studio?: undefined; }; } | { name: string; address: string; phones: { home: { num: number; }; office?: undefined; studio?: undefined; }; } | { ...; })[]' is not assignable to type 'Contact[]'.
Type '{ name: string; address: string; phones: { home: { num: number; }; office: { num: number; }; studio?: undefined; }; } | { name: string; address: string; phones: { home: { num: number; }; office?: undefined; studio?: undefined; }; } | { ...; }' is not assignable to type 'Contact'.
Type '{ name: string; address: string; phones: { home: { num: number; }; office: { num: number; }; studio?: undefined; }; }' is not assignable to type 'Contact'.
Types of property 'phones' are incompatible.
Type '{ home: { num: number; }; office: { num: number; }; studio?: undefined; }' is not assignable to type 'PhoneNumberDictionary'.
Property 'studio' is incompatible with index signature.
Type 'undefined' is not assignable to type '{ num: number; }'.ts(2345)
라고 뜹니다
제가 잘못 따라한게 있을가요?