#3 TypeScript

#3.0 Introduction

TypeScript ใ„ด JS ๊ธฐ๋ฐ˜์˜ ์–ธ์–ด, ๊ฑฐ์˜ ๋น„์Šทํ•จ ใ„ด Strongly Typed Programming Language

const plus = (a, b) => a + b;
const a = 1;
const b = 'hi';
console.log(plus(a,b)) // 1hi;

=> JS๋Š” ํƒ€์ž…์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์Œ โ‡’ ์ž‘์€ ์‹ค์ˆ˜๋“ค์ด ๋ฐœ์ƒํ•ด๋„ ๊ทธ๋Œ€๋กœ ์ง„ํ–‰๋จ

โ‡’ TypeScript ํƒ„์ƒ ( ํƒ€์ž…์„ ์ง€์ •ํ•ด ์ฝ”๋“œ ์‹คํ–‰ ์ „ ์•ฝ๊ฐ„์˜ ๋ณดํ˜ธ ์ž‘์šฉ )

const plus = (a:number, b:number) => a + b;
const a = 1;
const b = 'hi';
console.log(plus(a+b)) // error !!

TS Playground - An online editor for exploring TypeScript and JavaScript


#3.1 DefinitelyTyped

REACT์— ํƒ€์ž… ์Šคํฌ๋ฆฝํŠธ ์„ค์น˜

  1. ์ „๋ถ€ ์ง€์šฐ๊ณ  ์ƒˆ๋กœ ๋งŒ๋“ค๊ธฐ

    npm create-react-app my-app --template typescript
    // or
    yarn create react-app my-app --template typescript
  2. ๋ชจ๋“  ํƒ€์ž… ์Šคํฌ๋ฆฝํŠธ ์„ค์น˜

    npm install --save typescript @types/node @types/react @types/react-dom @types/jest @types/style-components // ... ํ•„์š”ํ•œ ์Šคํฌ๋ฆฝํŠธ ์ถ”๊ฐ€
    // or
    yarn add typescript @types/node @types/react @types/react-dom @types/jest @types/style-components

#3.2 Typing the Props

ํƒ€์ž… ์Šคํฌ๋ฆฝํŠธ์— Props ์„ค์ •ํ•˜๊ธฐ

interface object์˜ shape๋ฅผ ์„ค๋ช…


#3.3 Optional Props

๋ชจ๋“  Props๋ฅผ ํ•„์ˆ˜ ์ƒํƒœ๋กœ ๋†“์„ ํ•„์š”๊ฐ€ ์—†๋‹ค.

Optional Props๋ฅผ ์„ค์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด interface ๋‚ด๋ถ€ props๋ช… ๋’ค์— ?๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

ํ•„์ˆ˜์ธ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ดˆ๊นƒ๊ฐ’์„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋‹ค.


#3.4 State

๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฒ•์€ ๊ฐ™๋‹ค.

์ดˆ๊นƒ๊ฐ’์˜ ํƒ€์ž…๊ณผ ๋‹ค๋ฅด๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•จ & ์—ฌ๋Ÿฌ๊ฐœ์˜ ํƒ€์ž…์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ

์ดˆ๊นƒ๊ฐ’์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด undefined


#3.5 Forms

event ํ•จ์ˆ˜ ๋ถ€๋ถ„์—์„œ event์— ํƒ€์ž…์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ


#3.6 Themes

styled.d.ts ํŒŒ์ผ (d.ts โ‡’ declaration file)

theme.ts ํŒŒ์ผ์— ์›ํ•˜๋Š” ํ…Œ๋งˆ ์„ค์ •

index.tsx์—์„œ ํ…Œ๋งˆ๋ฅผ import

app.tsx์—์„œ props๋กœ ๋ฐ›์•„ ์‚ฌ์šฉํ•œ๋‹ค.


#3.7 Recap

TypeScript ์— ์—†๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด npm i โ€”save-dev @types/styled-components ์™€ ๊ฐ™์ด ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์— ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ญ”์ง€ ์•Œ๋ ค์ฃผ๋Š” install์„ ์ˆ˜ํ–‰ํ•ด์•ผํ•œ๋‹ค.

SyntheticEvent (ํ•ฉ์„ฑ ์ด๋ฒคํŠธ)

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋Š” ๋ชจ๋“  ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋™์ผํ•˜๊ฒŒ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ Syntetic Event ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค.

Keyboard Events ex) onKeyDown onKeyPress onKeyUp

Focus Events ex) onFocus onBlur

Form Events ex) onChange onInput onInvalid onReset onSubmit

Generic Events ex) onError onLoad

etc..

SyntheticEvent - React


Last updated

Was this helpful?