The Basic Cheatsheet
์ ์ ์กฐ๊ฑด
์ ์ ์กฐ๊ฑด์ ๋ณด๋ฉด์ ์๊ฒ ๋ ์ฌ์ค ์ ๋ฆฌ
TypeScript์ ์ฅ์
TypeScript๋ฅผ ์ด๋ค๋ฉด JavaScript์์ unit test๋ฅผ ์ธ ํ์๊ฐ ์๋ค. TypeScript๊ฐ ์๋์ ์ผ๋ก ์ฒดํฌํด ์ค๋ค.
This is especially useful when youโre using a UI library and need to transform data. For example, if youโre using React, youโll need to transform data in state updates.
we can use mapped types like Readonly
to convert one type to another type.
// Readonly<...> makes each property readonly
type Todo = Readonly<{
id: number
text: string
done: boolean
}>
// ์ <...>์ ์๋์ ๊ฐ๋ค.
type Todo = {
readonly id: number
readonly text: string
readonly done: boolean
}
readonly
๋ array์ push์๋ ๊ฐ์ด ์ฐ์ผ ์ ์๋ค.
Type assertions
let someValue: any = "this is a string";
let strLength: number = (<string>someValue).length;
// as Type์ ๋ ์ถ์ฒํ๋ค.
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;
์ถ์ฒ https://ts.chibicode.com/todo/
Function Components
Hooks
useState
const [user, setUser] = React.useState<IUser | null>(null);
default value์ธ null๊ณผ ํจ๊ป |
๋ฅผ ์ฌ์ฉํ๋ union type
์ ๋ง์ hook๋ค์ ์ฌ์ฉํ๋ค.
useRef
initial value๊ฐ ์๋ ref container ์์ฑํ ๋
const ref1 = useRef<HTMLElement>(null!);
const ref2 = useRef<HTMLElement | null>(null);
ref1๋ readonly์ฉ react๊ฐ ๊ด๋ฆฌํ built-in ref attributes์ ์ ๋ฌํ๊ธฐ ์ํ ๊ฒ? (Response handles for your current value setting)
ref2๋
ref2.current
๋ฅผ mutableํ๊ฒ ํด์ฃผ๊ณ ์ฌ์ฉ์๊ฐ ์ง์ ๊ด๋ฆฌํ๋instance variables
๋ฅผ ์ํ ๊ฒ์ด๋ค.
๋ค? ๋ญ๋ผ๊ตฌ์ฌ???
useEffect
ํจ์ ๋๋ undefined ์ด์ธ์ ๋ค๋ฅธ ๊ฒ์ด return ๋์ง ์๋๋ก ์ฃผ์ํ๋ค.
setTimeout
์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ number๊ฐ return๋๊ธฐ ๋๋ฌธ์ curly brace๋ก ๊ผญ ๊ฐ์ธ์.
useRef
function TextInputWithFocusButton() {
// ์ด๊ธฐ๊ฐ์ null, HTMLInputElement๋ฅผ ์ฐพ๊ณ ์๋ค๊ณ TypeScript์ ๋งํจ
const inputEl = React.useRef<HTMLInputElement>(null);
const onButtonClick = () => {
// strict null ์ฒดํฌ๋ inputEl์ current๊ฐ ์กด์ฌํ๋์ง ํ์ธํด์ผํ๋ค.
// ์กด์ฌํ๋ฉด HTMLInputElement type์ด๋ฏ๋ก ๋ฐ๋ผ์ method focus๋ฅผ ๊ฐ์ง๋ค. ๐ Yay
if (inputEl && inputEl.current) {
inputEl.current.focus();
}
};
return (
<>
{/* ๋ํ inputEl๋ input์์๋ง ์ฌ์ฉํ ์ ์๋ค? focus ๋๋ฌธ์ธ๊ฐ */}
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}
useReducer
reducer action์ Discriminated Unions ์ฌ์ฉํ๋ค. return type์ ์ ์ํ๋ ๊ฒ์ ์ ๋ ์์ง๋ง์.
type AppState = {};
type Action =
| { type: "SET_ONE"; payload: string }
| { type: "SET_TWO"; payload: number };
export function reducer(state: AppState, action: Action): AppState {
switch (action.type) {
case "SET_ONE":
return {
...state,
one: action.payload, // `payload` is string
};
case "SET_TWO":
return {
...state,
two: action.payload, // `payload` is number
};
default:
return state;
}
}
Discriminated Unions(์ ๋์จ ์๋ณ)์ singleton types, union types, type guards, ๋ฐ type aliases์ ๊ฒฐํฉํ์ฌ discriminated unions, tagged unions, ๋๋ algebraic(๋์์) data type์ผ๋ก ๋ถ๋ฆฌ๋ ๊ณ ๊ธ ํจํด์ ๋น๋ํ ์ ์๋ค.
interface Square {
kind: "square";
size: number;
}
interface Rectangle {
kind: "rectangle";
width: number;
height: number;
}
interface Circle {
kind: "circle";
radius: number;
}
๊ฒฐํฉํ interface๋ฅผ ์ ์ธํ๋ค.
๊ณตํต์ ์ผ๋ก kind
๋ผ๋ ํ๋กํผํฐ๋ฅผ ๊ฐ์ง๊ณ ์๋๋ฐ ์ด๋ฅผ discriminant
ํน์ tag
๋ผ๊ณ ๋ถ๋ฅธ๋ค.
type Shape = Square | Rectangle | Circle;
๊ฒฐํฉ์ฐ
function area(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.height * s.width;
case "circle": return Math.PI * s.radius ** 2;
}
}
discriminated union์ ์ฌ์ฉํ์!
useReducer์์๋ type
์ด discriminant
ํน์ tag
๊ฐ ๋๋ ๊ฒ์ด๊ณ
์ด์ ์ ๋ดค๋ ์ฝ๋๋ค์ ์๊ฐํ๋ฉด App component์์ status
๋ฅผ ์์๋ก ๋ค ์ ์์ ๊ฑฐ ๊ฐ๋ค.๐คญ
export interface VerifyingAuth {
status: 'verifying';
}
export interface UnsignedAuth {
status: 'unsigned';
inProgress?: 'signing';
error?: Error;
}
export interface SignedAuth {
status: 'signed';
inProgress?: 'passwordChanging' | 'unsigning';
error?: Error;
token: string;
user: UserInfo;
}
export type Auth = VerifyingAuth | UnsignedAuth | SignedAuth;
<AuthProvider api={api}>
{(auth: Auth) => {
switch (auth.status) {
case 'verifying':
return <div>...verifying</div>;
case 'unsigned':
return (
<Switch>
<Route exact path="/" component={LoginPage} />
<Redirect to="/" />
</Switch>
);
case 'signed':
return (
<MissionProvider>
<Switch>
<Route path="/dashboard" component={DashboardPage} />
<Route path="/missions/:missionId" component={MissionPage} />
<Redirect to="/dashboard" />
</Switch>
</MissionProvider>
);
default:
return <div>Unknown auth status...</div>;
}
}}
</AuthProvider>
Custom Hooks
Last updated
Was this helpful?