๋์ ์ธ์ด์ธ JavaScript๋ run time์ type์ด ๊ฒฐ์ ๋์ด ์ค๋ฅ๊ฐ ๋ฐ๊ฒฌ๋๋ฉด ์ฌ์ฉ์๊ฐ ๋ณผ ์ ์๊ฒ ๋๋ค.
Java๋ TypeScript๊ฐ์ ์ ์ ์ธ์ด๋ compile time์ type์ด ๊ฒฐ์ ๋๊ธฐ ๋๋ฌธ์ code ์งค ๋๋ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ ค๋ ์ค๋ฅ๊ฐ ์ฌ์ฉ์์๊ฒ ๋ณด์ด๋ ๊ฑธ ๋ง์ ์ ์๋ค.
TypeScript๋ JavaScript์ type์ ๋ช
ํํ๊ฒ ํ๋ ์ธ์ด์ด๋ค.
JavaScript๋ dynamic typing์ด ๊ฐ๋ฅํด์ ์์ ๋์ ์ ์ฐ์ฑ์ด ๋์๋ฐ, ์ด๋ project๊ฐ ํด์๋ก ๋ถ์ ์ ์ธ ํจ๊ณผ๊ฐ ํฌ๋ค.
์ด๋ TypeScript๋ JavaScript์ ๋ฌ๋ฆฌ ์ ์ฐ์ฑ์ ํ์ฉํ์ง ์๊ณ error๋ก ์ก์์ค๋ค.
๋๋ถ์ error message๊ฐ ๋ ๋ช
ํํ๋ค.
let ๊ธ์ : string = "sentence"
let ๋์ด : number = 30;
let ์ฑ์ธ์ฌ๋ถ : boolean = true;
let ๋ฐฐ์ด1 : number[] = [1, 2, 3];
let ๋ฐฐ์ด2 : Array<number> = [1, 2, 3];
let ์ผ์ฃผ์ผ1 : string[] = ["mon", "tue", "wed"];
let ์ผ์ฃผ์ผ2 : Array<string> = ["mon", "tue", "wed"];
๋ฐฐ์ด ์์์ type์ด ๋ค๋ฅผ ๋ ์ฌ์ฉํ๋ค.
let ๋ฐฐ์ด : [string, number]; // ์ฒซ ๋ฒ์งธ ์์๋ ๋ฌธ์, ๋ ๋ฒ์งธ ์์๋ ์ซ์
๋ฐฐ์ด[1].toLowerCase(); // ์ซ์ type์ ์๋ method์ด๋ฏ๋ก error ๋ฐ์
void๋ ํจ์์์ ์๋ฌด๊ฒ๋ ๋ฐํํ์ง ์์ ๋ ์ฌ์ฉํ๋ค.
function sayHello() : void {
console.log("Hello")
}
never๋ ํญ์ error๋ฅผ ๋ฐํํ๊ฑฐ๋ ๋๋์ง ์๋ ํจ์์ ์ฌ์ฉํ๋ค.
function showError() : never {
throw new Error();
}
function inLoop() : never {
while (true) {
// loop loop
}
}
๋น์ทํ ๊ฐ๋ค๋ผ๋ฆฌ ๋ฌถ๋๋ค.
type์ enum์ผ๋ก ์ ์ธํ๋ฉด enum์ ์ ์ ๊ฐ๋ง ์
๋ ฅํ ์ ์๋ค.
enum Os {
Window = "win",
Ios = "ios",
Android = "and"
}
let myOs: Os;
myOs = Os.Window
let a: null = null;
let b: undefined = undefined;
object์๋ ํน์ ์์ฑ ๊ฐ์ ๋ํ ์ ๋ณด๊ฐ ์๋ค.
property๋ฅผ ์ ์ํด์ ๊ฐ์ฒด๋ฅผ ํํํ ๋ interface๋ฅผ ์ฌ์ฉํ๋ค.
let user : object;
user = {
name: "mai",
age: 30,
}
console.log(user.name) // name์ ๋ค์๊ณผ ๊ฐ์ error ๋ฐ์ 'Property 'name' does not exist on type 'object'.'
// โ interface ์ฌ์ฉ
interface User {
name : string;
age : number;
}
let user : User = {
name : "mai",
age : 30,
}
๊ฐ์ ์ ๊ทผ์ ๊ฐ๋ฅํ์ง๋ง ์์ ์ ํ ์ ์๋ค.
interface User {
name : string;
age : number;
readonly birthYear : number;
}
let user : User = {
name : "mai",
age : 30,
birthYear : 2000,
}
user.birthYear = 1990; // birthYear๋ readonly์ด๊ธฐ ๋๋ฌธ์ error ๋ฐ์
interface Score {
[grade : number] : string; // ์ด ํ์์ผ๋ก ์ฌ๋ฌ ๊ฐ ์
๋ ฅํ ์ ์์
}
let studens : Score = {
1 : "A",
2 : "B"
}
interface Add {
(num1 : number, num2 : number) : number;
}
const add : Add = function(x, y) {
return x + y;
}
add(10, 20);
interface IsAdult {
(age : number) : boolean;
}
const a : IsAdult = (age) => {
return age > 19;
}
a(33) // true
implements
๋ฅผ ์ฌ์ฉํ๋ค.
interface Car {
color : string;
wheels : number;
start() : void;
}
class Lucid implements Car {
color;
wheels = 4;
constructor(c : string) {
this.color = c;
}
start() {
console.log("Engine Start");
}
}
const b = new Lucid("ivory");
console.log(b); // Lucid: { "wheels": 4, "color": "ivory" }
b.start(); // "Engine Start"
extends
๋ฅผ ์ฌ์ฉํ๋ค.
์ฌ๋ฌ ๊ฐ์ interface๋ฅผ extends ํ ์๋ ์๋ค.
interface Car {}
interface Toy {}
interface ToyCar extends Car, Toy {}
// ๐ต name์ด ์ ํ์ ๋งค๊ฐ๋ณ์์
function hello(name? : string) {
return `Hello, ${name || "world"}`;
}
const result1 = hello();
const result2 = hello("Mai");
// โ ๋งค๊ฐ๋ณ์์ default ๊ฐ์ ์ฃผ๋ฉด
function hello(name = "world") {
return `Hello, ${name}`;
}
const result1 = hello();
const result2 = hello("Mai");
์ฐธ๊ณ ๋ก ๋งค๊ฐ๋ณ์๊ฐ ์ฌ๋ฌ ๊ฐ์ผ ๋, ์ ํ์ ๋งค๊ฐ๋ณ์๊ฐ ํ์ ๋งค๊ฐ๋ณ์๋ณด๋ค ์์ ์ค๋ฉด ์ ๋๋ค. ๋ง์ฝ ์ ํ์ ๋งค๊ฐ๋ณ์๋ฅผ ๋จผ์ ์ฐ๊ณ ์ถ๋ค๋ฉด undefined
๋ฅผ ๋ฐ์ ์ ์๊ฒ ์ ๊ณ , ๋ช
์์ ์ผ๋ก undefined๋ฅผ ์ ๋ฌํด์ผ ํ๋ค.
function hello(age : number | undefined, name : string) : string {
if (age !== undefined) {
return `Hello, ${name}. You are ${age}.`;
} else {
return `Hello, ${name}`;
}
}
console.log(hello(undefined, "Mai"));
function add(...nums : number[]) {
return nums.reduce((result, num) => result + num, 0);
}
add(1, 2, 3); // 6
add (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // 55
ํจ์์ ์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์ ์๋ฆฌ์ this
๋ฅผ ์
๋ ฅํ๊ณ type์ ์
๋ ฅํ๋ค.
interface User {
name : string;
}
const Mai : User = {name : "Mai"}
function showName(this : User) {
console.log(this.name)
}
const a = showName.bind(Mai); // bind๋ก this๋ฅผ Mai ๊ฐ์ฒด๋ก ๊ฐ์
a();
ํจ์ overload๋ก ์ ๋ฌ ๋ฐ์ ๋งค๊ฐ๋ณ์์ ๊ฐ์๋ type์ ๋ฐ๋ผ ๋ค๋ฅธ ๋์์ ํ๊ฒ ํ๋ค.
interface User {
name : string;
age : number;
}
function join(name : string, age : string) : string; // ์ฒซ ๋ฒ์งธ overload ํจ์
function join(name : string, age : number) : User; // ๋ ๋ฒ์งธ overload ํจ์
function join(name : string, age : number | string) : User | string {
if (typeof age === "number") {
return {
name,
age,
};
} else {
return "๋์ด๋ ์ซ์๋ก ์
๋ ฅํด ์ฃผ์ธ์.";
}
}
const mai: User = join("Mai", 30);
const ian : string = join("Ian", "30");
์ ํด์ง string ๊ฐ์ ๊ฐ์ง ๊ฒ์ '๋ฌธ์์ด literal type'์ด๋ผ๊ณ ํ๋ค.
type
์ enum
์ฒ๋ผ ์ธ ์ ์๋ค.
const userName1 = "Ian";
let userName2 : string = "Mai";
type Job = "police" | "developer" | "scientist";
interface User {
name : string;
job : Job;
}
const user : User = {
name : "Ian",
job : "developer" // job property๋ ์์์ ์ ์ธํ string๋ง ์ธ ์ ์์
}
interface HighSchoolStudent {
name : string;
grade : 1 | 2 | 3; // 1, 2, 3 ์ค์ ํ๋๋ง ์
๋ ฅํ ์ ์์
}
interface Car {
name : "car";
color : string;
start() : void;
}
interface Mobile {
name : "mobile";
color : string;
call() : void;
}
function getGift(gift : Car | Mobile) {
if (gift.name === "car") {
gift.start();
} else {
gift.call();
}
}
๊ต์ฐจ type์ ์ฌ๋ฌ ๊ฐ์ type์ ํ๋๋ก ํฉ์ณ์ค๋ค.
union type์ด '๋๋'์ด๋ผ๋ฉด, ๊ต์ฐจ type์ 'and'๋ฅผ ์๋ฏธํ๋ค.
๊ทธ๋์ ๋ชจ๋ ์์ฑ์ ๊ธฐ์
ํด ์ฃผ์ด์ผ ํ๋ค.
interface Car {
name : string;
start() : void;
}
interface Toy {
name : string;
color : string;
price : number;
}
const ToyCar : Toy & Car = {
name : "ํ์",
start() {},
color : "blue",
price : 1000,
};
์ฝ๋ฉ์ ํ - ํ์
์คํฌ๋ฆฝํธ ์ฐ๋ ์ด์ & ํ์ ๋ฌธ๋ฒ 10๋ถ ์ ๋ฆฌ
์ฝ๋ฉ์๋ง - TypeScript ๊ฐ์ข