#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๋ฅผ ์„ค๋ช…

interface DummyProps{
	text: string;
}

function Dummy({ text } : DummyProps){  // text์˜ ํƒ€์ž…์€ DummyProps์—์„œ ์ฐพ์•„๋ผ
	return <H1>{text}</H1>
}

#3.3 Optional Props

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

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

interface CircleProps{
	bgColor: string; // ํ•„์ˆ˜
	borderColor?: string; // ์˜ต์…˜
}

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

1. props์— ์ดˆ๊นƒ๊ฐ’ ์ฃผ๊ธฐ
...
borderColor={borderColor ?? "white"}
...

2. function์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— default ๊ฐ’ ์ถ”๊ฐ€
function Circle({ text= "default text"}){
...
}

#3.4 State

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

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

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

const [value, setValue] = useState(0); // ์ดˆ๊นƒ๊ฐ’์˜ ํƒ€์ž…์— ๋”ฐ๋ผ value์˜ ํƒ€์ž…๋„ ์ •ํ•ด์ง
setValue("hi") // error
setValue(true) // error
setValue(3) // value === 3

//// ๋งŒ์•ฝ ํƒ€์ž…์„ ์—ฌ๋Ÿฌ๊ฐœ ์„ค์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด
const [value, setValue] = useState<number|string>(0);
// value์˜ ํƒ€์ž…์€ number๋‚˜ string์ž„ !

#3.5 Forms

import React, { useState } from "react";

function App() {
  const [value, setValue] = useState("");
  const onChange = (event: React.FormEvent<HTMLInputElement>) => {
    const {
      currentTarget: { value },
    } = event;
    setValue(value);
  };
  const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    console.log("hello", value);
  };

  return (
    <div>
      <form onSubmit={onSubmit}>
        <input value={value} onChange={onChange} type="text" placeholder="username" />
        <button>Log In</button>
      </form>
    </div>
  );
}

export default App;

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?