2. TypeScript
ํ์ต ํค์๋
REPL
TypeScript
interface vs Type
ํ์ ์ถ๋ก
Union Type vs Intersection Type
Optional Parameter
ํ์ค์ ์ผ๋ก TypeScript๋ฅผ ์ฐ๋ ๊ฐ์ฅ ํฐ ์ด์ ๋ vscode ์๋์์ฑ + ์ค์๊ฐ ์ค๋ฅ ๊ฒ์ฌ
์ค๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ฒฝ์ฐ d.ts ํ์ผ๋ง ๋ฐ๋ก ํจํค์ง๋ก ์ ๊ณต๋๋ค.
ํ์
๋ณ์นญ (type alias)
๋๊ฐ์ ํ์ ์ ํ ๋ฒ ์ด์ ์ฌ์ฌ์ฉํ๊ฑฐ๋ ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ๋ถ๋ฅด๊ณ ์ถ์ ๊ฒฝ์ฐ์ ์ฌ์ฉํ๋ค.
ํ์ ์ ์ํ ์ด๋ฆ์ด๋ค.
type Point = {
x: number;
y: number;
};
// ์์ ์ฌ์ฉํ ์์ ์ ๋์ผํ ์ฝ๋์
๋๋ค
function printCoord(pt: Point) {
console.log("The coordinate's x value is " + pt.x);
console.log("The coordinate's y value is " + pt.y);
}
๊ฐ์ฒด ํ์ ๋ฟ์ด ์๋ ๋ชจ๋ ํ์ ์ ๋ํด ์๋ก์ด ์ด๋ฆ์ ๋ถ์ฌํ ์ ์๋ค.
// ex)
type ID = number | string;
๋จ์ง ๋ณ์นญ์ผ๋ก, ๋์ผ ํ์ ์ ๋ํด ๊ฐ๊ธฐ ๊ตฌ๋ณ๋๋ '์ฌ๋ฌ ๋ฒ์ '์ ๋ง๋๋๊ฒ ์๋๋ค.
๋ณ๋๋ก ์ด๋ฆ ๋ถ์ธ ํ์ ์ ์๋ก ์์ฑํ๋ ๊ฒ.
type UserInputSanitizedString = string;
function sanitizeInput(str: string): UserInputSanitizedString {
return sanitize(str);
}
// ๋ณด์ ์ฒ๋ฆฌ๋ฅผ ๋ง์น ์
๋ ฅ์ ์์ฑ
let userInput = sanitizeInput(getInput());
// ๋ฌผ๋ก ์๋ก์ด ๋ฌธ์์ด์ ๋ค์ ๋์
ํ ์๋ ์์ต๋๋ค
userInput = "new input";
interface
๊ฐ์ฒด ํ์ ์ ๋ง๋๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ.
interface Point {
x: number;
y: number;
}
function printCoord(pt: Point) {
console.log("The coordinate's x value is " + pt.x);
console.log("The coordinate's y value is " + pt.y);
}
ํ์
๋ณ์นญ๊ณผ ์ธํฐํ์ด์ค์ ์ฐจ์ด์
๋น์ท ํ์ง๋ง ๋๋ ทํ ์ฐจ์ด๋,
ํ์ ์ ์ ํ๋กํผํฐ๋ฅผ ์ถ๊ฐํ๋๋ก ๊ฐ๋ฐฉ๋ ์ ์๊ณ ,
์ธํฐํ์ด์ค์ ๊ฒฝ์ฐ ํ์ฅ๋ ์ ์๋ค๋ ์ ์ ๋๋ค.
interface Animal {
name: string
}
interface Bear extends Animal {
honey: boolean
}
const bear = getBear()
bear.name
bear.honey
type๋ Bear ํ์ ์ ๋ง๋ค ์ ์์ง๋ง
ํ๋กํผํฐ๊ฐ ์ถ๊ฐ๋๋ extends์ ๊ฐ๋ ์ด ์๋๊ณ ๊ต์งํฉ์ ์ด์ฉํ ๋ฐฉ์์ด๋ผ์
ํ์ฅ ํน์ ๊ฐ๋ฐฉ์ด ๋ ์๋ ์๋ค๊ณ ํํํ ๊ฒ ๊ฐ๋ค.
type Animal = {
name: string
}
type Bear = Animal & {
honey: Boolean
}
const bear = getBear();
bear.name;
bear.honey;
๊ธฐ์กด์ ์ธํฐํ์ด์ค์ ์ ํ๋๋ฅผ ์ถ๊ฐํ๊ธฐ
interface Window {
title: string
}
interface Window {
ts: TypeScriptAPI
}
const src = 'const a = "Hello World"';
window.ts.transpileModule(src, {});
ํ์
์ ์์ฑ๋ ๋ค์๋ ๋ฌ๋ผ์ง ์ ์๋ค
type Window = {
title: string
}
type Window = {
ts: TypeScriptAPI
}
// Error: Duplicate identifier 'Window'.
์ธ์ ์ด๋ค๊ฑธ ์จ์ผํ ์ง ์ ๋ชจ๋ฅด๊ฒ ๋ค๋ฉด ์ฐ์ interface ์ฐ๊ณ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ๋ type ์ฐ๋ผํจ.
์ ํด์ง ๊ฐ์ผ๋ก ํ์ ์ ์ง์ ํ ์๋ ์๋ค. ์ด๋ฐ ํ์ ์ Union์์ ์ ์ฉํ๊ฒ ์ฐ์ธ๋ค.
let category: 'food';
category = 'food';
๋ฐฐ์ด๋ณด๋ค ๊น๊นํ Tuple ํ์ ์ด ์๋ค.
let anythings: any[];
anythings = ['hp', 256];
let pair: [string, number];
pair = ['hp', 256];
Union Type
์ฌ๋ฌ ํ์ ์ค ํ๋.
type bool = true | false;
let flag: bool;
flag = true;
flag = false;
flag = 3; // ์๋ฌ๋จ
๋งค๊ฐ๋ณ์ ์ ํํ ๋ ๋งค์ฐ ์ ์ฉ
type Category = 'food' | 'toy' | 'bag';
function fetchProducts({ category }: { category: Category }) {
console.log(`Fetch ${category}`);
}
๊ธฐ๋ณธ๊ฐ์ ์ก์์ค ์ ์๋ค.
function greeting(name: string = 'world'): string {
return `Hello, ${name}`;
}
๋งค๊ฐ๋ณ์๊ฐ ์ค๋ธ์ ํธ์ผ ๊ฒฝ์ฐ
function greeting({ name, age }: {
name: string;
age?: number;
}): string {
return age ? `${name} (${age})` : name;
}
โ ์ด๋ ๊ฒ ์ฐ๋ฉด ts-node์์ ์์ฝ๊ฒ๋ ํด์ ๋ถ๊ฐ.
์๋์ฒ๋ผ ํ ์ค๋ก ๋ถ์ฌ์ ์ฐ๊ฑฐ๋, type ๋ฑ์ผ๋ก ๋ฐ๋ก ์ก์์ฃผ๋ฉด ๋๋ค.
function greeting({ name, age }: { name: string; age?: number; }): string {
return age ? `${name} (${age})` : name;
}
//
// ๋ ๊น๋
type Human = {
name: string;
age?: number;
};
function greeting({ name, age }: Human): string {
return age ? `${name} (${age})` : name;
}
greeting()
greeting({ name: 'ํ๊ธธ๋' })
greeting({ name: 'ํ๊ธธ๋', age: 13 })
Intersection Type
๊ต์งํฉ
type Combined = { a: number } & { b: string };
type Conflicting = { a: number } & { a: string };
Combined ์ ๋ง์น ํ๋์ ๊ฐ์ฒด ๋ฆฌํฐ๋ด ํ์
์ผ๋ก ์์ฑ๋ ๊ฒ ์ฒ๋ผ a ์ b ๋ ๊ฐ์ ์์ฑ์ ๊ฐ์ง.
๊ต์งํฉ๊ณผ ์ ๋์ธ์ ์ฌ๊ท์ ์ธ ์ผ์ด์ค์์ ์ถฉ๋์ ์ผ์ผ์ผ์ Conflicting์ ์ผ์ผํด.
Intersection Types
interfaces๋ฅผ ์ฌ์ฉํ๋ฉด ๋ค๋ฅธ ์ ํ์ ํ์ฅํ์ฌ ์ ์ ํ์ ๊ตฌ์ถํ ์ ์๋ค.
interface Colorful {
color: string;
}
interface Circle {
radius: number;
}
type ColorfulCircle = Colorful & Circle;
function draw(circle: Colorful & Circle) {
console.log(`Color was ${circle.color}`);
console.log(`Radius was ${circle.radius}`);
}
// okay
draw({ color: "blue", radius: 42 });
// oops
draw({ color: "red", raidus: 42 }); // ์คํ ใ
ใ
Generics
์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ปดํฌ๋ํธ๋ฅผ ๊ตฌ์ถํ๋๋ฐ ์ฌ์ฉํ๋ ๋๊ตฌ.
ํ์ฌ์ ๋ฐ์ดํฐ์ ๋ฏธ๋์ ๋ฐ์ดํฐ ๋ชจ๋๋ฅผ ๋ค๋ฃฐ ์ ์๋ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ ๋ค.
๋จ์ผ ํ์ ์ด ์๋ ๋ค์ํ ํ์ ์์ ์๋ํ๋ ์ปดํฌ๋ํธ๋ฅผ ์์ฑํ ์ ์๋ค.
identity ํจ์๋ ์ธ์๋ก ๋ฌด์์ด ์ค๋ ๊ทธ๋๋ก ๋ฐํํ๋ ํจ์.
์ ๋ค๋ฆญ์ด ์๋ค๋ฉด ํจ์์ ํน์ ํ์
์ ์ค์ผํ๋ค.
function identity(arg: number): number {
return arg;
}
//or
function identity(arg: any): any {
return arg;
}
any๋ ์ด๋ค ํ์ ์ด๋ ๋ฐ์ ์ ์์ง๋ง ๋ฐํํ๋ ๊ฐ์ ํ์ ์ ์๋๋ค.
number๊ฐ์ ๋๊ฒจ๋ any ํ์ ์ด ๋ฐํ๋๋ค๋ ์๋ฏธ.
์ฐ๋ฆฌ๋ ๋ฌด์์ด ๋ฐํ๋๋์ง ํ์ํ๊ธฐ ์ํด ์ธ์์ ํ์
์ ์บก์ฒ
ํ ๋ฐฉ๋ฒ์ด ํ์ํฉ๋๋ค.
๊ฐ์ด ์๋ ํ์ ์ ์ ์ฉ๋๋ ํ์ ๋ณ์๋ฅผ ์ฌ์ฉํ ๊ฒ์ ๋๋ค.
function identity<Type>(arg: Type): Type {
return arg;
}
Type๋ผ๋ ํ์ ๋ณ์๋ฅผ ์ถ๊ฐํ๋ค.
Type์ ์ ์ ๊ฐ ์ค ์ธ์์ ํ์
์ ์บก์ฒ
ํ๊ณ ์ด ์ ๋ณด๋ฅผ ๋์ค์ ์ฌ์ฉํ ์ ์๊ฒ ํ๋ค.
์ฌ๊ธฐ์๋ ๋ฐํ ํ์ ์ผ๋ก ๋ค์ ์ฌ์ฉ.
์ด ๋ฒ์ ์ identity ํจ์๋ ํ์
์ ๋ถ๋ฌธํ๊ณ ๋์ํ๋ฏ๋ก ์ ๋ค๋ฆญ
์ด๋ผ ํ ์ ์์ต๋๋ค.
any๋ฅผ ์ฐ๋ ๊ฒ๊ณผ๋ ๋ค๋ฅด๊ฒ ์ธ์์ ๋ฐํ ํ์ ์ number๋ฅผ ์ฌ์ฉํ ์ฒซ ๋ฒ์งธ identity ํจ์๋งํผ ์ ํ.
(์ฆ, ์ด๋ค ์ ๋ณด๋ ์์ง ์๋๋ค)
๋ ์ข์ ํ์ ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋จธ๋ก ๋ง๋๋ 11๊ฐ์ง ํ
Last updated