2.Typescript
REPL(Read-Eval-Print-Loop)
ํฐ๋ฏธ๋์์ ๊ฐ๋จํ๊ฒ REPL์ ์ฐ๊ณ ์ถ์ผ๋ฉด ts-node๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
npx ts-node
Interface vs Type
Interface์ Type Alias ๋ค์ ๋น์ทํด๋ณด์ด๋ ๊ฒ์ ์ฌ์ค์ด๋ ์ฌ์ฉ์ ์ ์ ํ์
์ด ํ ์ ์๋ ๊ฒ์ ์ธํฐํ์ด์ค๋ ํ ์ ์๋ค. ํ ์ ์๋ ๊ฒ ์ค ํ๋๋ ์ธํฐํ์ด์ค๋ ์ ์ธ์ ๋ณํฉ
ํ ์ ์๋ค๋ ์ ์ด๋ค.
์๋์ ํ์ ๋ค์ ๋ด๊ฐ ํ์ฌ ์ค๋ฌด์์ ์ฌ์ฉํ๊ณ ์๋ ํ๋ก๋ชจ์ ํ์ ๋ค ์ค ์ผ๋ถ๋ถ์ ๊ฐ์ ธ์๋ค.
Interface
์ธํฐํ์ด์ค๋ ๋ช ์์ ์ผ๋ก ํ์ฅ์ ์ฌ์ฉํด์ ์ ์๋ฅผ ํด์ค ์๋ ์๋ค.
export interface PromotionCoupon {
promoId: string,
name: string,
issuanceType: PromotionIssuance,
createdAt: string,
expiryAt: string,
code: string,
discountType: PromotionDiscount,
discountRate: number,
}
export interface PromotionCouponList extends PromotionCoupon {
targetCount: number,
issuanceCount: number,
}
๋ Type Alias์์ ํ์ง ๋ชปํ๋ ์ ์ธ ๋ณํฉ
์ ํ ์ ์๋ค.
interface ButtonInterface {
onInit():void;
onClick():void;
}
...
interface ButtonInterface {
onToggle():void;
}
์์ ๊ฐ์ด ์ ์ธ ๋์์ ๋ ์ธํฐํ์ด์ค๋ ์ค๋ฅ๊ฐ ๋์ง ์๋๋ค.
Type
ํ์ ์ ๋ณํฉํ๊ณ ์ ํ๋ ํ์ ๊ณผ & ๊ธฐํธ๋ฅผ ํ์ฉํด์ ํ์ ๋ณํฉ์ด ๊ฐ๋ฅํ๋ค.
export type PromotionOwnerList = {
uuid: string,
email: string,
nickname: string,
userPromoStatusId: string,
promoId: string,
maxUseAt: string,
usedAt: string | null,
isUsed: boolean,
createdAt: string,
isChecked?: boolean,
}
export type PromotionCandidateList = User & {
provider: Provider,
emailCert: boolean,
privacyAgree: boolean,
eventAgree: boolean,
createdAt: string,
isChecked?: boolean,
}
type ButtonType = {
onInit():void;
onClick():void;
}
// [์ค๋ฅ]
// 'ButtonType' ์๋ณ์๊ฐ ์ค๋ณต๋์๋ค.
type ButtonType = {
onToggle():void;
}
ํ์ ์์๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
ํ์
์ถ๋ก ๊ณผ ํ์
๋จ์ธ
ํ์
์ถ๋ก
let x = 3;
let y = 'hello';
์์ ๊ฐ์ด x
์ ๋ํ ํ์
์ ์ง์ ํ์ง ์๋๋ผ๋ number
ํ์
์ผ๋ก y
์ ๋ํ ํ์
์ ์ง์ ํ์ง ์๋๋ผ๋ string
ํ์
์ผ๋ก ๊ฐ์ฃผ๋๋ค.
ํ์
๋จ์ธ
let assertion:any = "ํ์
์ด์ค์
์ 'ํ์
์ ๋จ์ธ'ํ๋ค.";
// ๋ฐฉ๋ฒ 1: assertion ๋ณ์์ ํ์
์ string์ผ๋ก ๋จ์ธ ์ฒ๋ฆฌ
let assertion_count:number = (<string>assertion).length;
let assertion:any = "ํ์
์ด์ค์
์ 'ํ์
์ ๋จ์ธ'ํ๋ค.";
// ๋ฐฉ๋ฒ 2: assertion ๋ณ์์ ํ์
์ string์ผ๋ก ๋จ์ธ ์ฒ๋ฆฌ
let assertion_count:number = (assertion as string).length;
๋ณดํต JSX์์ ๋ง์ด ์ฐ๋ ํ์ ๋จ์ธ์๋ divํ๊ทธ์์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ์ํ์์ ํด๋น div์์ input์์ ๊ฐ์ ๊ฐ์ ธ์์ผ ํ๋ ๊ฒฝ์ฐ์ ํ์ ๋จ์ธ์ ๋ง์ด ์ฌ์ฉํ๋ค.
const onChangeCategory = useCallback((e: React.FormEvent<HTMLDivElement>) => {
const { value } = (e.target as HTMLInputElement);
...
}, []);
Union Type vs Intersection Type
Union Type
์ ๋์จ ํ์ (Union Type)์ A์ด๊ฑฐ๋ B์ด๋ค ๋ผ๋ ์๋ฏธ์ ํ์ ์ด๋ค.
๋ณดํต ์ง์ ๋ ๊ฐ ์ด์ธ์ ๊ฐ์ด ๋ค์ด๊ฐ์ง ์์์ผ ํ ๋ ์ ๋์จ ํ์ ์ ์ง์ ํด์ ๋ง์ด ์ฌ์ฉํ๋ ํธ์ด๋ค.
export type PromotionCategory = 'COUPON' | 'LINK';
export type PromotionDiscount = 'FREE' | 'DISCOUNT';
Intersection Type
์ธํฐ์น์ ํ์ (Intersection Type)์ ์ฌ๋ฌ ํ์ ์ ๋ชจ๋ ๋ง์กฑํ๋ ํ๋์ ํ์ ์ด๋ค.
export type PromotionCandidateList = User & {
provider: Provider,
emailCert: boolean,
privacyAgree: boolean,
eventAgree: boolean,
createdAt: string,
isChecked?: boolean,
}
interface Person {
name: string;
age: number;
}
interface Developer {
name: string;
skill: number;
}
type Capt = Person & Developer;
Optional Parameter
JavaScript์์๋ ์กด์ฌํ์ง ์๋ ํ๋กํผํฐ์ ์ ๊ทผํ์์ ๋, ๋ฐํ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์๊ณ undefined ๊ฐ์ ์ป๊ฒ ๋๋ค.
export type PromotionCandidateList = User & {
provider: Provider,
emailCert: boolean,
privacyAgree: boolean,
eventAgree: boolean,
createdAt: string,
isChecked?: boolean, <- option
}
function printName(obj: { first: string; last?: string }) {
// ...
}
// ๋ ๋ค OK
printName({ first: "Bob" });
printName({ first: "Alice", last: "Alisson" });
References
Last updated