2. TypeScript
ํค์๋
REPL
TypeScript
ํ์ ์ถ๋ก
Interface vs Type
Union Type vs Intersection Type
Optional Parameter
ํ์
์คํฌ๋ฆฝํธ, ์ ์ฐ๋๊ฐ?
ํธ๋ฆฌํ ์๋์์ฑ
์ค์๊ฐ ์ค๋ฅ๊ฒ์ฌ
REPL
Read-Eval-Print-Loop
์ฌ์ฉ์์ ์ ๋ ฅ์ ๋ฐ์ ํ๊ฐํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํดํ๋ ์ธํฐ๋ ํฐ๋ธ ํ๋ก๊ทธ๋๋ฐ ํ๊ฒฝ
ts-node๋ฅผ ์คํํด ํ์ ์คํฌ๋ฆฝํธ REPL ์ฌ์ฉ ๊ฐ๋ฅ
npx ts-node
ํ์
์ถ๋ก
ํ์ ์คํฌ๋ฆฝํธ๋ ์๋ฃ์ ๋ค์ ํ์ ์ ๋ช ์
ํ์ ์ ๋ช ์ํ์ง ์์๋ ๊ฐ์ ํตํด ์๋์ผ๋ก ํ์ ์ ๊ฒฐ์ ํด์ฃผ๊ธฐ๋ ํจ
const name: string = 'ํ๊ธธ๋';
const name = 'ํ๊ธธ๋';
// ์์ ์ฝ๋์ ์๋์ฝ๋๋ ๊ฐ๋ค.
ํ์
์์ํ์
let name: string;
let age: number;
let isAdult: boolean;
name = "ํ๊ธธ๋";
age = 20;
isAdult = true;
๋ฐฐ์ด
number[]
๋๋ Array<number>
์ ํํ๋ก ์ฌ์ฉ
let numbers: number[];
numbers = [1, 2, 3];
ํํ
๊ธธ์ด์ ํ์ ์ด ๊ณ ์ ๋ ๋ฐฐ์ด
์์์ ๋ง์ง ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ผ๋ฉด ์๋ฌ ํ์
let user1: [number, string, string];
user1 = [1, 'setType77@example.com', '1q2w3e4r'];
ํ๊ณ์
push
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ ์์ ์๊ด์์ด ๋ฐฐ์ด์ ๊ธธ์ด๊ฐ ๋์ด๋จ
let user1: [number, string, string];
user1 = [1, 'setType77@example.com', '1q2w3e4r'];
user1.push('fake'); // ์๋ฌ์์ด ์ปดํ์ผ๋ฉ๋๋ค.
ํด๊ฒฐ๋ฐฉ์
ํํ ์ ์ธ์์ readonly
์ฌ์ฉ
type UserInfo = readonly [number , string, string ,string , boolean]; // ๋ฐฐ์ด ์์ readonly ์ ์ฉ
let userInfo1 : UserInfo = [1, 'user1@example.com' , 'abcd123' , '1999-01-01' , true];
userInfo1.push('hello'); // Error
any
์ด๋ค ๊ฐ์ ํ์ ์ด any์ด๋ฉด ํ์ ๊ฒ์ฌ๋ฅผ ์คํํ์ง ์์
ํจ์
๋งค๊ฐ๋ณ์์ ๋ฆฌํด๊ฐ์ ํ์ ์ ํ๊ธฐํ ์ ์์
// ๋งค๊ฐ๋ณ์ ํ์
ํ๊ธฐ
function greet(name: string) {
console.log("Hello, " + name.toUpperCase() + "!!");
}
// ๋ฐํ ํ์
ํ๊ธฐ
function getFavoriteNumber(): number {
return 26;
}
๋ฌธ๋งฅ์ ํตํด ์ต๋ช ํจ์์ ํ์ ์ ์ถ๋ก ํ ์ ์๋ค๋ฉด, ์๋์ผ๋ก ํ์ ์ด ๋ถ์ฌ๋จ
const names = ["Alice", "Bob", "Eve"];
names.forEach(function (s) {
console.log(s.toUppercase());
//Property 'toUppercase' does not exist on type 'string'. Did you mean 'toUpperCase'?
});
names.forEach((s) => {
console.log(s.toUppercase());
//Property 'toUppercase' does not exist on type 'string'. Did you mean 'toUpperCase'?
});
๊ฐ์ฒด
let human: {
name: string;
age: number;
};
human = { name: 'ํ๊ธธ๋', age: 13 };
Optional parameter
๊ฐ์ฒด์ ์์ฑ์ ์ต์ ํํ๊ฑฐ๋ ํจ์์ ๋งค๊ฐ๋ณ์๋ฅผ ์ต์ ํํ ์ ์์
function printName(obj: { first: string; last?: string }) {
// ...
}
// ๋ ๋ค OK
printName({ first: "Bob" });
printName({ first: "Alice", last: "Alisson" });
function greeting(name?: string): string {
return `Hello, ${name || 'world'}`;
}
Union vs. Intersection
๊ธฐ์กด์ ํ์ ์ ๊ธฐ๋ฐ์ผ๋ก ๋ค์ํ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด์ ์๋ก์ด ํ์ ์ ๋ง๋ค ์ ์์
Union Type
์ซ์ ๋๋ ๋ฌธ์๋ฅผ ๋ฐ์ ์ ์๋ ํจ์
function printId(id: number | string) {
console.log("Your ID is: " + id);
}
// OK
printId(101);
// OK
printId("202");
// ์ค๋ฅ
printId({ myID: 22342 });
์ ๋์ธ์ ์ฌ์ฉํ ๋๋ ํด๋น ์ ๋์ธ ํ์ ์ ๋ชจ๋ ๋ฉค๋ฒ์ ๋ํด ์ ํจํ ์์ ๋ง ํ์ฉ๋จ
string | number
์ ๋์ธ ํ์
์ ๊ฒฝ์ฐ, string ํ์
์๋ง ์ ํจํ ๋ฉ์๋๋ ์ฌ์ฉํ ์ ์์
function printId(id: number | string) {
console.log(id.toUpperCase());
// Property 'toUpperCase' does not exist on type 'string | number'.
// Property 'toUpperCase' does not exist on type 'number'.
}
typeof๋ฅผ ์ด์ฉํด ๋ถ๊ธฐ ์ฒ๋ฆฌ
function printId(id: number | string) {
if (typeof id === "string") {
// ์ด ๋ถ๊ธฐ์์ id๋ 'string' ํ์
์ ๊ฐ์ง๋๋ค
console.log(id.toUpperCase());
} else {
// ์ฌ๊ธฐ์์ id๋ 'number' ํ์
์ ๊ฐ์ง๋๋ค
console.log(id);
}
}
Intersection Type
์ฌ๋ฌ ํ์ ์ ๋ชจ๋ ๋ง์กฑํ๋ ํ๋์ ํ์
interface Person {
name: string;
age: number;
}
interface Developer {
name: string;
skill: number;
}
type Capt = Person & Developer;
Capt
ํ์
์ ์๋์ ๊ฐ์ด ์ ์๋จ
{
name: string;
age: number;
skill: string;
}
Type vs. Interface
type
๋๊ฐ์ ํ์ ์ ์ฌ์ฌ์ฉํ๊ฑฐ๋ ๊ฐ์ฒด, ์ ๋์ธ ํ์ ๋ฑ์ ๋ ๊ฐ๋จํ ์ด๋ฆ์ผ๋ก ๋ถ๋ฅด๊ณ ์ถ์ ๊ฒฝ์ฐ ํ์ ์ ๋ณ์นญ์ ๋ง๋ค ์ ์์
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);
}
printCoord({ x: 100, y: 100 });
type ID = number | string;
interface
type
๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๊ฐ์ฒด ํ์
์ ๋ง๋๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ
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);
}
printCoord({ x: 100, y: 100 });
type๊ณผ interface์ ์ฐจ์ด์
์ธํฐํ์ด์ค๋ ๊ฐ์ฒด์๋ง ์ฌ์ฉ๋๋ฉฐ, ์์ํ์ ์ ์ด๋ฆ์ ๋ถ์ฌํ๋ ๋ฐ๋ ์ฌ์ฉํ ์ ์์
์ธํฐํ์ด์ค๋ extends
๋ฅผ ํตํด ํ์ฅ, ํ์
์ ๊ต์งํฉ์ ํตํด ํ์ฅ
interface Animal {
name: string
}
interface Bear extends Animal {
honey: boolean
}
const bear = getBear()
bear.name
bear.honey
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'
Last updated