๐Ÿ“’[TypeScript ๋งˆ์Šคํ„ฐ with Webpack & React] Step11.TypeScript ํด๋ž˜์Šค

๊ถŒ์šฉ์ค€ยท2023๋…„ 12์›” 6์ผ
0
post-thumbnail

1. TypeScript์—์„œ ํด๋ž˜์Šค ์–ด๋…ธํ…Œ์ด์…˜ ์ฒ˜๋ฆฌํ•˜๊ธฐ


// TypeScript์—์„œ์˜ ํด๋ž˜์Šค ์–ด๋…ธํ…Œ์ด์…˜ ์ฒ˜๋ฆฌ

class Player{
    first: string;
    last: string;
    score: number = 0;
    constructor(first: string, last: string){
        this.first = first;
        this.last = last;
    }
}

const elton = new Player("Elton", "Steele");

2. readonly ํด๋ž˜์Šค ํ”„๋กœํผํ‹ฐ



class Player{
    readonly first: string;
    readonly last: string;
    score: number = 0;
    constructor(first: string, last: string){
        this.first = first;
        this.last = last;
    }
}

const elton = new Player("Elton", "Steele");
elton.first = "elton"; //error

3. public


๊ธฐ๋ณธ์ ์œผ๋กœ public์ด๋ฏ€๋กœ ๋”ฐ๋ผ์„œ public ์ ‘๊ทผ ์ œ์–ด์ž๋Š” ํ•„์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ, ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ๋„ ๋ณ€๊ฒฝ, ์ ‘๊ทผ,์“ฐ๊ธฐ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋ช…ํ™•ํžˆ ์•Œ๋ฆฌ๊ธฐ์œ„ํ•ด public์„ ์‚ฌ์šฉํ•˜๋ฉฐ ์–ด๋””์—์„œ๋“  ์“ธ ์ˆ˜ ์žˆ๋‹ค.


class Player{
    public readonly first: string;
    public readonly last: string;
    public score: number = 0;
    constructor(first: string, last: string){
        this.first = first;
        this.last = last;
    }
}

const elton = new Player("Elton", "Steele");

5. private


private์€ ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ ๋˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์˜ค์ง ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

private์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

ํ”„๋กœํผํ‹ฐ ๋˜๋Š” ๋ฉ”์„œ๋“œ ์•ž์—
1. private์„ ์ถ”๊ฐ€
2. #์ถ”๊ฐ€


class Player{
    public readonly first: string;
    public readonly last: string;
    private score: number = 0; // 1. private
    #score: number = 0; // 2. private 
    constructor(first: string, last: string){
        this.first = first;
        this.last = last;
    }
}

const elton = new Player("Elton", "Steele");
elton.score; // error 1. private
elton.#score; // error 2. private

6. ํŒŒ๋ผ๋ฏธํ„ฐ ํ”„๋กœํผํ‹ฐ ๋‹จ์ถ• ๊ตฌ๋ฌธ


  1. private์˜ ์†Œ์Šค์ฝ”๋“œ์™€ ๊ฐ™์€ ํšจ๋ ฅ์„ ๋ฐœ์ƒํ•œ๋‹ค.
    ์ด๋ ‡๊ฒŒ ๋‹จ์ถ•ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ๊ณ ์žˆ์ž

class Player{
    // public readonly first: string;
    // public readonly last: string;
    // private score: number = 0;
    constructor(
        public first: string,
        public last: string,
        private score: number){

    }
}

const elton = new Player("Elton", "Steele", 100);

7. Getter์™€ Setter


class Player{
    public readonly first: string;
    public readonly last: string;
    private _score: number = 0;
    constructor(
        public first: string,
        public last: string,
        private _score: number){}

    get score(): number{
        return this._score;
    }

    set score(newScore: number){
        if(newScore < 0){
            throw new Error("Can not");
        }
        return this._score;
    }
}

const elton = new Player("Elton", "Steele", 100);

elton.score = 99;
console.log(elton.score); // 99

8. protected ์ œ์–ด์ž


์ƒ์† ์ž‘์—… ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ protected๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์™ธ๋ถ€์—์„œ ์•ก์„ธ์Šคํ•˜๋Š” ๊ฒŒ ๋ถˆ๊ฐ€๋Šฅํ•˜์ง€๋งŒ
์ž์‹ ํด๋ž˜์Šค๋Š” ์•ก์„ธ์Šค๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.


class Player{
    constructor(
        public first: string,
        public last: string,
        protected _score: number){

    }
}

class SuperPlayer extends Player{
    public isAdmin:boolean = true;
    maxScore(){
        this._score = 999;
    }
}

public์€ ์–ด๋””์„œ๋‚˜ ์•ก์„ธ์Šค ๊ฐ€๋Šฅํ•˜๊ณ 
private์€ ์ •์˜๋œ ํ•ด๋‹น ํด๋ž˜์Šค์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ณ 
protected๋Š” ์ •์˜๋œ ํด๋ž˜์Šค์™€ ๊ทธ๋กœ๋ถ€ํ„ฐ ์ƒ์†ํ•œ ๋ชจ๋“  ํด๋ž˜์Šค์—์„œ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค.

9. ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค


์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋Š” ๊ฐ„๋‹จํ•˜์ง€๋งŒ ์•„์ฃผ ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

interface Colorful{
    color: string;
}

interface Printable{
    print(): void;
}

class Bike implements Colorful{
    constructor(public color: string){}
}

class Jacket implements Colorful, Printable{
    constructor(public brand:string, public color:string){}

    print(){
        console.log(` ${this.color} ${this.brand} jacket`);
    }
}

10. Abstract ํด๋ž˜์Šค


TypeScript์—์„œ์˜ Abstract์€ ์ด ์ž์ฒด๋กœ๋Š” ๋” ์ด์ƒ ์ƒˆ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค๋Š”๊ฑธ ์˜๋ฏธํ•œ๋‹ค.
abstract ํด๋ž˜์Šค๋Š” ํŒจํ„ด์„ ์ •์˜ํ•˜๊ณ  ์ž์‹ ํด๋ž˜์Šค์—์„œ ์‹œํ–‰๋ผ์•ผ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋งŒ ์กด์žฌํ•˜๊ณ  abstract ํด๋ž˜์Šค๋Š” ํ™•์žฅํ•˜๋Š” ํด๋ž˜์Šค์— ๊ธฐ๋Šฅ ์ฆ‰, ํŒจํ„ด์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฉ”์„œ๋“œ ์•ž์— abstract๋ฅผ ๋ถ™์ด๋ฉด ์ž์‹ ํด๋ž˜์Šค๊ฐ€ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ˜๋“œ์‹œ ์‹œํ–‰ํ•ด์•ผํ•œ๋‹ค.

abstract class Employee{
    constructor(public first: string, public last: string){}
    abstract getPay(): number; //Employee๋ฅผ ํ™•์žฅํ•˜๋Š” ๋ชจ๋“  ํด๋ž˜์Šค์— ์กด์žฌํ•ด์•ผํ•œ๋‹ค
    greet(){
        console.log("hi");
    }
}

class FullTimeEmployee extends Employee{
    constructor(first: string, last: string, private salary: number){
        super(first,last);
    }
    getPay(): number {
        return this.salary;
    }
    
}

class PartTimeEmployee extends Employee{
    constructor(
        first: string, 
        last: string, 
        private hourlyRate: number, 
        private hoursWorked: number){
        super(first,last);
    }
    getPay(): number {
        return this.hourlyRate * this.hoursWorked;
    }
}

const betty = new FullTimeEmployee("Betty", "White", 9500);
console.log(betty.getPay()); // 9500 
console.log(betty.greet());  // hi

ํ™•์žฅํ•  abstract ํด๋ž˜์Šค์™€ interface๋Š” ๋™์‹œ์— ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค

profile
Brendan Eich, Jordan Walke, Evan You, ๊ถŒ์šฉ์ค€

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