#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 !!
#3.1 DefinitelyTyped
REACT์ ํ์
์คํฌ๋ฆฝํธ ์ค์น
์ ๋ถ ์ง์ฐ๊ณ ์๋ก ๋ง๋ค๊ธฐ
npm create-react-app my-app --template typescript
// or
yarn create react-app my-app --template typescript
๋ชจ๋ ํ์
์คํฌ๋ฆฝํธ ์ค์น
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์ !
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..