> For the complete documentation index, see [llms.txt](https://organization-bht.gitbook.io/it/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://organization-bht.gitbook.io/it/undefined-1/react-with-typescript/type-script/1./undefined.md). Agents can also search and read this documentation through its [MCP server](https://organization-bht.gitbook.io/it/~gitbook/mcp).

# 타입스크립트 기초 실습문제

## 실습문제 1) 타입주석 작성

* animal과 human의 타입주석을 작성하시오

```javascript
let animal:??, human:??;
animal = ['panda','cat','wombat','frog','hamster'];
human = {name : 'mkm', age : 25, height: 180.5, married : false}
```

## 실습문제 2) 타입주석 작성

* zoo의 타입주석을 작성하시오

```javascript
let zoo:??;
zoo = {
    animals : ['panda','cat','wombat','frog','hamster'],
    total   : 120,
    isClosed : false,
    location : "용인",
    masterInfo : {
        name : 'mkm',
        age : 25,
        height : 180.5
    }
};
```

## 실습문제 3) 다음 조건에 만족하는 타입 만들기

```typescript
let response: ??;
```

* `reponse` 변수는 항상   다음 3 값중 한가지만을 가집니다.

1. `[200 , "response success"]`
2. `[404, "Page not found"]`
3. `[500, "server error"]`

* 0번 인덱스에 숫자값만 와야합니다.
* 1번 인덱스에는 문자열값만 와야합니다.
* 2번 인덱스부터 새로운 값이 추가될 경우 반드시 에러를 발생시켜야 합니다.

## 실습문제 4) 다음 조건에 맞는 타입 지정하기

```javascript
let array:??;
array = [12345, 'mkm', true, undefined]; // ok
array = ['mkm' , 12345, true]; // ok
array.push({name : 'mkm'}); // ok
```

* array변수는 튜플이 아닙니다.&#x20;
* array변수는 unknown\[] , any\[]타입이 아닙니다.

## 실습문제 5)  다음 조건에 맞는 타입 지정하기

```typescript
let favorite:?? = '치킨'; // ok
favorite:?? = '햄버거'; // ok 
favorite:?? = '피자'; // x
favorite:?? = '짜장면'; // x
```

* favorite에는 치킨과 햄버거는 저장 가능하지만 그 외 어떤 값도저장할 수 없음.

## 실습문제 6) 자료형 맞추기

<pre class="language-typescript"><code class="lang-typescript"><strong>//Q1. test1 변수에는
</strong><strong>//    undefined를 넣어도 문제가 없습니다.
</strong>//    '1234'를  넣어도 문제가 없습니다.
//    1234를 넣으면 컴파일 에러가 발생합니다.
//    true를 넣으면 컴파일 에러가 발생합니다.
//    test1의 타입은 ?
let test1: ???  = undefined; // ok
test1 = '1234'; // ok
test1 = 1234; // compile error
test1 = true; //compile error

//Q2. test2 변수에는 모든 값을 담을 수 있습니다.
//    true, 1234, undefined등을 넣어도 문제가 없습니다.
//    test1에 test2를 담을 수 있습니다.
//    test2는 UnionType이 아닙니다.
//    test2의 타입은?
let test2 : ??? = true; //ok
test2 = 1234;// ok
test1 = test2; // ok

//Q3. test3 변수에는모든 값을 담을 수 있습니다.
//    test1에 test3를 담으면 컴파일 에러가 발생합니다.
//    test3은 UnionType이 아닙니다.
//    test3의 타입은?
let test3 : ??? = undefined; // ok
test3 = 1234; // ok
test3 = true; // ok
test1 = test3; // compile error
</code></pre>
