Open suehdn opened 1 year ago
Q. GameUser(user)에서 오류가 발생하지 않도록 코드를 적절하게 변경해주세요.
type UserName = "Kim" | "Lee" | "Jea";
interface UserInfo {
userName: UserName;
age: number;
}
function GameUser(userInfo: UserInfo) {
/* ... */
}
const user = {
userName: "Kim",
age: 30,
};
GameUser(user); // 오류
Q. 아래의 코드를 보고 변수에 추론 되는 타입을 작성해주세요.
const a:{1:1|2} = {
1 : 2
};
const study1 = {
language : 'TypeScript',
total : 6
};
const study2 = {
language : 'TypeScript' as const,
total : 6 as const
};
const study3 = {
language : 'TypeScript',
total : 6
} as const;
타입 : { 1:1|2; }
타입 : { language: string; total: number;}
타입 : {language: "TypeScript";total: 6;}
타입 : {readonly language: "TypeScript";readonly total: 6;}
Q. frong
은 person
의 모든 속성을 갖습니다. 아래 코드에서 isProgrammer
가 true인 경우 {job: 'programmer'}
속성이 추가되는 새로운 객체 frong을 완성해주세요.
declare let isProgrammer: boolean;
const person = {name: 'Minhee', age: 13};
const frong = ???
참고: frong의 타입은 다음과 같이 추론됩니다.
const frong: {
name: string;
age: number;
job?: string;
}
Q. 아래 코드의 의도는 빈 객체인 devcourse객체에 track의 first와 second를 추가 하려는 코드입니다.
아래 코드의 console.log(devcourse)
는 에러가 발생하지 않고 정상적으로 객체가 합쳐져 정상작동합니다.
하지만 타입스크립트의 Property 'first', 'second' does not exist on type '{}'.
에러가 발생합니다.
에러가 발생하지 않고 객체를 합칠 수 있는 해결방법을 작성해주세요.
const track = {
first: 'frong',
second: 'backdung'
}
const devcourse = {};
devcourse.first = track.first; // Property 'first' does not exist on type '{}'.
devcourse.second = track.second; // Property 'second' does not exist on type '{}'.
console.log(devcourse); // { "first": "frong", "second": "backdung" }
Q. readonly 접근 제어자에 대한 설명으로 옳은 문장을 모두 고르세요.
Q. 다음 코드가 에러가 나는 이유는 무엇일까요? 그 이유와 오류를 고칠 수 있는 방법을 작성해주세요
type Restaurant = '대낚식당' | '육품' | '딘타이펑';
interface HotPlace {
restaurant: Restaurant;
location: string;
}
function search( restaurant: HotPlace ) { /* ... */ }
search( { restaurant: '육품', location: 'Gangnam' } );
const food = {
restaurant: '대낚식당',
location: 'Gangnam',
};
search( food ); //오류 발생
const user: UserInfo = {
userName: "Kim",
age: 30,
};
{...person,...(isProgrammer?{job: 'programmer'}:{})}
const devcourse = {...track}
1,3
restaurant에 string이 들어갈 수 없기 때문에
const food = {
restaurant: '대낚식당',
location: 'Gangnam',
} as const;
const user: UserInfo = {
userName: "Kim",
age: 30,
};
{...person, ...(isProgrammer ? {job: 'programmer'} : null)
const devcourse = {...track}
1
다은님 문제
const user: UserInfo = { ... }
혜수님 문제
{1: 1 | 2}
{language: string; total: number;}
{language: 'TypeScript'; total: 6}
{language: 'TypeScript'; total: 6}
종현님 문제
const devcourse = { ...track }
승희님 문제 1, 2, 3
주연님 문제
const food: HotPlace = { ... }
또는
const food = {
restaurant: '대낚식당' as const,
location: 'Gangnam'
}
다은님 퀴즈
user as UserInfo
혜수님 퀴즈
민희님 퀴즈
const frong = {
…person,
…(isProgrammer ? {job: 'programmer'} : {})
}
종현님 퀴즈 객체 전개를 통해 한번에 객체로 만든다.
const devcourse = {...track};
주연님 퀴즈 restaurant가 string으로 추론되어 food가 HotPlace로 할당되지 못하기 때문. 타입 선언을 추가한다.
const user: UserInfo = {
userName: "Kim",
age: 30,
};
const frong = {
...person,
...(isProgrammer ? { job: 'programmer' } : {})
}
1, 3
const food: HotPlace = {
restaurant: '대낚식당',
location: 'Gangnam',
};
혜수님
language : string,
total : number
language :"TypeScript",
total : 6
language :"TypeScript",
total : 6
민희님 ????어케해요..? isProgrammer && person..?ㅋㅋㅋㅎㅅㅎ 종현님
type Check = typeof track
const devcourse = {} as Check
승희님
1,2번
주연님
as const
데브코스 4기 프롱이들 이펙티브 타입스크립트 완독하기 🤩
아래 템플릿을 복사해서 퀴즈를 만들어주세요. 객관식, 주관식, 단답형 모두 상관없습니다!