๐Ÿ’ช TypeScript w/ ์ฝ”๋”ฉ์•™๋งˆ

[meษช]ยท2022๋…„ 4์›” 3์ผ
0

6-2. Todays I Want to learn. TypeScript

๋ชฉ๋ก ๋ณด๊ธฐ
1/1
post-thumbnail

Summary

๋ฐฐ๊ฒฝ

๋™์  ์–ธ์–ด์ธ JavaScript๋Š” run time์— type์ด ๊ฒฐ์ •๋˜์–ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ๊ฒฌ๋˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.
Java๋‚˜ TypeScript๊ฐ™์€ ์ •์  ์–ธ์–ด๋Š” compile time์— type์ด ๊ฒฐ์ •๋˜๊ธฐ ๋•Œ๋ฌธ์— code ์งค ๋•Œ๋Š” ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ ค๋„ ์˜ค๋ฅ˜๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์ด๋Š” ๊ฑธ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค.

TypeScript๋Š” JavaScript์˜ type์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๋Š” ์–ธ์–ด์ด๋‹ค.
JavaScript๋Š” dynamic typing์ด ๊ฐ€๋Šฅํ•ด์„œ ์ž์œ ๋„์™€ ์œ ์—ฐ์„ฑ์ด ๋†’์€๋ฐ, ์ด๋Š” project๊ฐ€ ํด์ˆ˜๋ก ๋ถ€์ •์ ์ธ ํšจ๊ณผ๊ฐ€ ํฌ๋‹ค.
์ด๋•Œ TypeScript๋Š” JavaScript์™€ ๋‹ฌ๋ฆฌ ์œ ์—ฐ์„ฑ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ  error๋กœ ์žก์•„์ค€๋‹ค.
๋•๋ถ„์— error message๊ฐ€ ๋” ๋ช…ํ™•ํ•˜๋‹ค.


1. Types

string, number, boolean, array

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"];

tuple

๋ฐฐ์—ด ์š”์†Œ์˜ type์ด ๋‹ค๋ฅผ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

let ๋ฐฐ์—ด : [string, number]; // ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋Š” ๋ฌธ์ž, ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋Š” ์ˆซ์ž
๋ฐฐ์—ด[1].toLowerCase(); // ์ˆซ์ž type์—” ์—†๋Š” method์ด๋ฏ€๋กœ error ๋ฐœ์ƒ

void, never

void๋Š” ํ•จ์ˆ˜์—์„œ ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

function sayHello() : void {
    console.log("Hello")
}

never๋Š” ํ•ญ์ƒ error๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜ ๋๋‚˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜์— ์‚ฌ์šฉํ•œ๋‹ค.

function showError() : never {
    throw new Error();
}

function inLoop() : never {
    while (true) {
        // loop loop
    }
}

enum

๋น„์Šทํ•œ ๊ฐ’๋“ค๋ผ๋ฆฌ ๋ฌถ๋Š”๋‹ค.
type์„ enum์œผ๋กœ ์„ ์–ธํ•˜๋ฉด enum์— ์ ์€ ๊ฐ’๋งŒ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

enum Os {
    Window = "win",
    Ios = "ios",
    Android = "and"
}

let myOs: Os;
myOs = Os.Window

null, undefined

let a: null = null;
let b: undefined = undefined;

2. Interface

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,
}

readonly

๊ฐ’์— ์ ‘๊ทผ์€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ˆ˜์ •์€ ํ•  ์ˆ˜ ์—†๋‹ค.

interface User {
    name : string;
    age : number;
    readonly birthYear : number;
}

let user : User = {
    name : "mai",
    age : 30,
    birthYear : 2000,
}

user.birthYear = 1990; // birthYear๋Š” readonly์ด๊ธฐ ๋•Œ๋ฌธ์— error ๋ฐœ์ƒ

๋ฌธ์ž์—ด index

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

class

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 {}

3. Function

์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜

// ๐ŸŽต 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 ๊ด€๋ จ

ํ•จ์ˆ˜์˜ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ž๋ฆฌ์— 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

ํ•จ์ˆ˜ 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");

4. Literal Types

type

์ •ํ•ด์ง„ 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 ์ค‘์— ํ•˜๋‚˜๋งŒ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Œ
}

์‹๋ณ„ ๊ฐ€๋Šฅํ•œ union types

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();
    }
}

intersection types

๊ต์ฐจ 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,
};



Endnote

๐Ÿ™‡ the source of this content

์ฝ”๋”ฉ์• ํ”Œ - ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์“ฐ๋Š” ์ด์œ  & ํ•„์ˆ˜ ๋ฌธ๋ฒ• 10๋ถ„ ์ •๋ฆฌ
์ฝ”๋”ฉ์•™๋งˆ - TypeScript ๊ฐ•์ขŒ

TS Playground

profile
๋Š๋ ค๋„ ํ•  ๊ฑฐ์•ผ.......

0๊ฐœ์˜ ๋Œ“๊ธ€