ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • TypeScript ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ -2
    ๐ŸŒ FrontEnd/TypeScript 2022. 11. 8. 20:33

    ์‹œ์ž‘ํ•˜๋ฉด์„œ

    ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ์ดˆ๋ฅผ ๊ณต๋ถ€ํ•ฉ๋‹ˆ๋‹ค.

     

    ํด๋ž˜์Šค ์ •์˜

    • ํด๋ž˜์Šค๋Š” implements ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • ํด๋ž˜์Šค๋Š” ๊ฐ์ฒด์™€ ๋น„์Šทํ•ด๋ณด์ด์ง€๋งŒ, ์ƒ์†์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    // TS
    
    interface Car{
        color:string;
        wheels:number;
        start():void;
    } // ์ƒ์„ฑ์ž(constructor) ๊นŒ์ง€๋Š” control ํ•  ์ˆ˜ ์—†๋‹ค
    
    class Bmw implements Car{ // ':' ๋ง๊ณ  implements ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค
        color; // TS ์—์„œ ์ƒ๋žตํ•˜๊ฒŒ ๋˜๋ฉด BMW ์•ˆ์˜ color๋ฅผ ์ฐพ์ง€ ๋ชปํ•œ๋‹ค. ๋ณ€์ˆ˜๋ฅผ ๊ผญ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผํ•œ๋‹ค.
        wheels=4; // TS ์—์„œ ์ƒ๋žตํ•˜๊ฒŒ ๋˜๋ฉด wheels๋ฅผ ์ฐพ์ง€ ๋ชปํ•œ๋‹ค. BMW class ์— Wheels ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผํ•œ๋‹ค.
        constructor(c:string){
            this.color=c;
        }
        start():void{
            console.log('go....!');
        }
    }
    
    const car1 = new Bmw('green');
    console.log(car1);
    car1.start();
    // JS
    
    class Bmw {
        constructor(c) { // constructor(์ƒ์„ฑ์ž ํ•จ์ˆ˜)๋Š” ์ฒ˜์Œ์— ์ž๋™์œผ๋กœ ์‹œ์ž‘ (๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค)
            this.wheels = 4; // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜
            this.color = c;
        }
        start() {
            console.log('go....!');
        }
    }
    
    const car1 = new Bmw('green'); // car1 ์€ ์ธ์Šคํ„ด์Šค
    console.log(car1);
    car1.start();

     

    extends (ํ™•์žฅ)

    interface Car{ // ๋ถ€๋ชจ ์ธํ„ฐํŽ˜์ด์Šค
        color:string;
        wheels:number;
        start():void;
    }
    
    interface Car2 extends Car{ // ์ž์‹ ์ธํ„ฐํŽ˜์ด์Šค
        door:number;
        stop(d:number):number;
    }
    
    // interface Car2{ // ์ž์‹ ์ธํ„ฐํŽ˜์ด์Šค
    //     color:string;
    //     wheels:number;
    //     door:number;
    //     start():void;
    //     stop():void;
    // }
    
    class Bmw implements Car{
        color;
        wheels=4;
        constructor(c:string){
            this.color=c;
        }
        start():void{
            console.log('Bmw go....!');
        }
    }
    
    const car1 = new Bmw('green');
    console.log(car1);
    car1.start();
    
    class Benz implements Car2{
        color='red';
        wheels=4;
        door;
        constructor(c:number){
            this.door=c;
        }
        start():void{
            console.log('Benz go....!');
        }
        stop(d:number):number{
            console.log('Benz stop....!');
            this.door = d;
            return this.door;
        }
    }
    
    const car2 = new Benz(4);
    car2.wheels = 10;
    car2.color = 'gray';
    car2.door = 100;
    console.log(car2);
    car2.start();
    
    let aaa:number = 0;
    aaa = car2.stop(200);
    console.log(aaa);
    
    // ๊ฒฐ๊ณผ
    
    [LOG]: Bmw: {
      "wheels": 4,
      "color": "green"
    } 
    [LOG]: "Bmw go....!" 
    [LOG]: Benz: {
      "color": "gray",
      "wheels": 10,
      "door": 100
    } 
    [LOG]: "Benz go....!" 
    [LOG]: "Benz stop....!" 
    [LOG]: 200

     

    extends (ํ™•์žฅ-2)

    // interface Pro{
    //     color:string;
    //     wheels:number;
    //     name:string;
    //     price:number;
    
    //     start():void;
    //     open():void;
    //     buy():void;
    // }
    
    interface Car {
        color:string;
        wheels:number;
        start():void;
    }
    
    interface Toy{
        name:string;
        open():void;
    }
    
    interface ToyCar extends Car, Toy {
        price:number;
        buy():void;
    }
    
    class Product implements ToyCar{
        color='red';
        wheels=4;
        name='doll';
        price=500;
    
        start():void{
            console.log('start....!');
        }
        open():void{
            console.log('open stop....!');
        }
        buy():void{
            console.log('buy stop....!');
        }
    }
    
    const person1 = new Product();
    
    console.log(person1);
    person1.start();
    person1.open();
    person1.buy();
    

     

    ํ•จ์ˆ˜ ํƒ€์ž… ์ •์˜ ๋ฐฉ๋ฒ•

    ์˜ˆ์ œ 1

    function add(num1:number , num2:number):number{
        return num1+num2;
    }
    console.log(add(10,20));  // 30
    
    function isAdult(age:number):boolean{
        return age > 19;
    }
    console.log(isAdult(30));  // true
    

    ์˜ˆ์ œ 2

    • ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด ์ฃผ์ง€ ์•Š๋Š”๋‹ค๋ฉด TS ๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
    function hello(name:string):string{
        return `Hello, ${name || "world"}`; // name์ด ๋“ค์–ด์˜ค๋ฉด name ์ถœ๋ ฅ, ์•„๋‹ˆ๋ผ๋ฉด "world" ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ
    }
    
    const result = hello();  // ์—๋Ÿฌ๋ฐœ์ƒ
    const result2 = hello('kim');
    console.log(result, result2);
    
    • ์ด๋ฅผ ์žก๊ธฐ ์œ„ํ•ด์„œ ์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜ ? ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    // ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๊ฒŒ ์ž‘์„ฑํ•˜๋ ค๋ฉด? ์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์ž!
    
    function hello(name?:string){  // ๋ณ€์ˆ˜? -> ์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜
        return `Hello, ${name || "world"}`;
    }
    
    const result = hello(); 
    const result2 = hello('kim'); 
    console.log(result, result2);
    
    • ์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  if๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด?
    function hello(name:string):string{
        if(name == undefined) {
            return `Hello, world`
        }else{
            return `Hello, ${name}`
        }
    }
    
    • ์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ดˆ๊ธฐ ๊ฐ’์„ ์ง€์ •ํ•ด์ค˜์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
    function hello2(name = 'world'):string{  // ๋ณ€์ˆ˜? -> ์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜
        return `Hello, ${name}`;
    }
    const result3 = hello2(); 
    const result4 = hello2('kim'); 
    console.log(result3, result4);
    

     

    ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜

    function add(...nums:number[]):number {
        return nums.reduce((result, num) => result + num);
    }
    
    console.log(add(1, 2, 3)); // 6
    console.log(add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); // 55
    

     

    ์˜ˆ์ œ 4

    interface User {
        name:string;
        age:number;
    }
    
    function join(name:string, age:number):User {
        return{
            name,
            age
        };
    };
    
    const person1:User = join('์‹ ํ˜•๋งŒ', 60);
    console.log(person1); 
    /*
    {
      "name": "์‹ ํ˜•๋งŒ",
      "age": 60
    } 
    */
    
    /*
    // interface ์—†์ด ์ง์ ‘ ๋ช…์‹œํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•
    
    function join(name:string, age:number):{name:string, age:number} {
        return{
            name,
            age
        };
    };
    
    const person1:{name:string, age:number} = join('์‹ ํ˜•๋งŒ', 60);
    console.log(person1); 
    
    */
    
    // this? ๊ฐ์ฒด๋‚˜ class ์—์„œ ์‚ฌ์šฉ
    

    ํ™”์‚ดํ‘œํ•จ์ˆ˜๋กœ ์ˆ˜์ •

    interface User {
        name:string;
        age:number;
    }
    
    interface Func {
        (name:string, age:number):User;
    }
    
    const join:Func = (name, age) => {
        return {
            name,
            age
        };
    }
    
    const person1:User = join('์‹ ํ˜•๋งŒ', 60);
    console.log(person1);
    

     

    ๋ฆฌํ„ฐ๋Ÿด(Literal types) / ์œ ๋‹ˆ์˜จ(Union types) / ๊ต์ฐจํƒ€์ž…(Intersection types)

    ๋ฆฌํ„ฐ๋Ÿด

    • ๋ฆฌํ„ฐ๋Ÿด์ด๋ž€? type์„ ์ƒ๋žตํ•˜๋”๋ผ๋„, ์ดˆ๊ธฐ๊ฐ’์— ๋”ฐ๋ผ type์ด ๊ณ ์ •๋œ๋‹ค๋Š” ๊ฒƒ ์ฆ‰, ์ •ํ™•ํ•œ ๊ฐ’์ด ์ง€์ •๋จ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฌธ์ž์—ด์ด๋‚˜ ์ˆซ์ž์— ์ •ํ™•ํ•œ ๊ฐ’์„ ์ง€์ •ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

    ์œ ๋‹ˆ์˜จ → | (๋˜๋Š”)

    • ์—ฌ๋Ÿฌ๊ฐœ์˜ ์„ ํƒ์ง€๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์ž…๋‹ˆ๋‹ค.
    const userName1 = 'Bob'; // type -> 'Bob' (์ƒ์ˆ˜ ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’ ์ž์ฒด๊ฐ€ type์ด ๋œ๋‹ค)
    let userName2 = 'Tom';  // type -> string
    let userName3: string | number;
    userName3 = '์œ ๋‹จ์ž';
    userName3 = 100;
    console.log(userName1, userName2, userName3);
    
    type Job = "police" | "developer" | "teacher";
    
    interface User{
        name : string;
        job : Job;
    }
    
    const user:User ={
        name : "Bob",
        job : "developer"   // job : "student" -> ์—๋Ÿฌ
    }
    console.log(user.name, user.job);
    

    ๊ต์ฐจํƒ€์ž… -> & (์—ฌ๋Ÿฌ๊ฐœ์˜ ํƒ€์ž…์„ ํ•˜๋‚˜๋กœ ํ•ฉ์นœ๋‹ค)

    interface Car{
        name : string;
        start(): void;
    }
    
    interface Toy{
        name : string;
        color : string;
        price: number;
    }
    
    const toyCar : Toy & Car ={
        name: '๋ฒ„์Šค',
        color: 'blue',
        price: 10000,
        start(){
            console.log(this.name, this.color, this.price+'์›');
        }
    }
    toyCar.start(); // "๋ฒ„์Šค",  "blue",  "10000์›"
    

     

    class ES6 (ํด๋ž˜์Šค ๋ณต์Šต)

    JS

    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(.js) ์—์„œ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    class Car{
        constructor(color){
            this.color = color;
        }
        start(){
            console.log(this.color + " car start!");
        }
    }
    
    const bmw = new Car("red");
    bmw.start();
    

    TS

    • .ts์—์„œ๋Š” color ๋ถ€๋ถ„์—์„œ ์˜ค๋ฅ˜ 3๊ฐœ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
    • ํด๋ž˜์Šค ๋งด๋ฒ„๋ณ€์ˆ˜๋ฅผ ๊ผญ ์„ ์–ธ์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.(.ts)
    class Car{
        color: string;
        constructor(color: string){
            this.color = color;
        }
        start(){
            console.log(this.color + " car start!");
        }
    }
    
    const bmw = new Car("red");
    bmw.start();
    

     

    public ์‚ฌ์šฉ

    public ์ด๋ž€? → ์ „์—ญ ์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

    class Car{
        // color: string;
        constructor(public color: string){ // constructor ๋‚ด๋ถ€์—์„œ public ์‚ฌ์šฉ
            this.color = color;
        }
        start(){
            console.log(this.color + " car start!");
        }
    }
    
    const bmw = new Car("red");
    bmw.start();
    

     

    readonly ์‚ฌ์šฉ(์ฝ๊ธฐ์ „์šฉ)

    class Car{
        // color: string;
        constructor(readonly color: string){
            this.color = color;
        }
        start(){
            console.log(this.color + " car start!");
        }
    }
    
    const bmw = new Car("red");
    bmw.start();
    // bmw.color='red';   // ์—๋Ÿฌ : ์ฝ๊ธฐ ์ „์šฉ์ด๋ผ ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค
    
    • public / read-only ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ƒ์ˆ˜(const)๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ดํ•ด๊ฐ€ ์‰ฝ์Šต๋‹ˆ๋‹ค.

     

    โญโญโญโญโญ ์ ‘๊ทผ ์ œํ•œ์ž(Access Modifier) โญโญโญโญโญ

    • public → ์ž์‹ ํด๋ž˜์Šค, ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค ๋ชจ๋‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ (์ƒ๋žตํ•˜๋ฉด ๊ธฐ๋ณธ๊ฐ’)
    • private → ํ•ด๋‹น ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ (์ž์‹ ํด๋ž˜์Šค ์‚ฌ์šฉ ๋ถˆ๊ฐ€)
    • protected → ์ž์‹ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ/ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์—์„œ ์ ‘๊ทผ ๋ถˆ๊ฐ€

    ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค → ์–ด์ฉŒ๊ณ  = new ddd → ์–ด์ฉŒ๊ณ .name ์ด๋Ÿฐ์‹์˜ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

    public

    class Car{
        public name:string = "๋˜ฅ์ฐจ";
        color:string;
        constructor(color:string){
            this.color=color;
        }
        public start(){ // ํ•จ์ˆ˜์—๋„ public ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
            console.log('๋ถ€๋ชจํด๋ž˜์Šค strat!!',this.name, this.color);
        }
    }
    
    class Bmw extends Car{
        constructor(color:string){
            super(color);
        }
        showName(){
           console.log('์ž์‹ํด๋ž˜์Šค strat!!', this.name , this.color);  // ๋ถ€๋ชจ์—๊ฒŒ์„œ ๋ฌผ๋ ค๋ฐ›์€ name, color (์ž์‹ ํด๋ž˜์Šค์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค)
        }
    }
    
    const mycar = new Bmw("red");
    mycar.name="์ƒˆ์ฐจ";  // ์ธ์Šคํ„ด์Šค์—์„œ ์ง์ ‘ ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค (์ธ์Šคํ„ด์Šค์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค)
    
    mycar.start(); // "๋ถ€๋ชจํด๋ž˜์Šค strat!!",  "์ƒˆ์ฐจ",  "red" 
    mycar.showName(); // "์ž์‹ํด๋ž˜์Šค strat!!",  "์ƒˆ์ฐจ",  "red"
    

    private

    class Car{
        private name:string = "๋˜ฅ์ฐจ";
        color:string;
        constructor(color:string){
            this.color=color;
        }
        private start(){ // ํ•จ์ˆ˜์—๋„ public ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
            console.log('๋ถ€๋ชจํด๋ž˜์Šค strat!!',this.name, this.color);
        }
    }
    
    class Bmw extends Car{
        constructor(color:string){
            super(color);
        }
        showName(){
           console.log('์ž์‹ํด๋ž˜์Šค strat!!', this.name , this.color);  // ๋ถ€๋ชจ์—๊ฒŒ์„œ ๋ฌผ๋ ค๋ฐ›์€ name, color (์ž์‹ ํด๋ž˜์Šค์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค)
        }
    }
    const car = new Car("pink");
    car.color = "blue";
    console.log(car.color);
    
    const mycar = new Bmw("red");
    mycar.name = "์ƒˆ์ฐจ";  // ์—๋Ÿฌ -> ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.
    mycar.color = "green"
    
    mycar.start(); // ์—๋Ÿฌ -> ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.
    mycar.showName(); // "์ž์‹ํด๋ž˜์Šค strat!!",  "์ƒˆ์ฐจ",  "red"
    

    protected

    class Car{
        protected name:string = "๋˜ฅ์ฐจ";
        color:string;
        constructor(color:string){
            this.color=color;
        }
        protected start(){
            console.log('๋ถ€๋ชจํด๋ž˜์Šค strat!!', this.name, this.color);
        }
    }
    
    class Bmw extends Car{
        constructor(color:string){
            super(color);
        }
        showName(){
            console.log('์ž์‹ํด๋ž˜์Šค strat!!', this.name , this.color);  //์ž์‹ ํด๋ž˜์Šค์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค
            super.start();
        }
    }
    const car = new Car("pink");
    car.color = "blue";
    
    const mycar = new Bmw("red");
    // mycar.name="์ƒˆ์ฐจ";  // ์—๋Ÿฌ : ์ธ์Šคํ„ด์Šค์—์„œ ์ง์ ‘ ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค
    // mycar.start();
    mycar.showName();
    

     

    ์ œ๋„ค๋ฆญ

    • ์ œ๋„ค๋ฆญ์€ ํŠน์ • ํƒ€์ž…์„ ์ด์šฉํ•ด์„œ ํ…œํ”Œ๋ฆฟ์ฒ˜๋Ÿผ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

    ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ „๋‹ฌ ์ธ์ž์˜ ํƒ€์ž…์ด ๋‹ค๋ฅผ๋•Œ…

    function getSize(arr: number[]): number{
       return arr.length;
    }
    
    const arr=[1,2,3]; 
    console.log(getSize(arr));  //3
    
    const arr2 = ['a','b','c','d','e'];
    console.log(getSize(arr2));  //์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค
    
    • ์œ ๋‹ˆ์˜จ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
    function getSize(arr: number[] | string[]): number{
       return arr.length;
    }
    
    const arr=[1,2,3]; 
    console.log(getSize(arr));  // 3
    
    const arr2 = ['a','b','c','d','e'];
    console.log(getSize(arr2)); // 5
    
    • ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•
    function ํ•จ์ˆ˜๋ช…<ํƒ€์ž…ํŒŒ๋ผ๋ฏธํ„ฐ>(๋งค๊ฐœ๋ณ€์ˆ˜:ํƒ€์ž…ํŒŒ๋ผ๋ฏธํ„ฐ):ํƒ€์ž…{}
    
    function getSize<T>(arr: T[]):number{
        return arr.length;
    }
    
    const arr = [1, 2, 3];
    console.log(getSize<number>(arr));
    
    const arr2 = ['a','b','c','d','e'];
    console.log(getSize<string>(arr2)); 
    
    const arr3 = [true,false,false,true];
    console.log(getSize<Boolean>(arr3));
    
    // any ๋ฅผ ์“ฐ๋Š” ๊ฒฝ์šฐ๋Š”?? type script๋ฅผ ์“ธ ์ด์œ ๊ฐ€ ์—†๋‹ค. ๊ทธ๋ƒฅ vanila๋กœ ์“ฐ๋ฉด ๋œ๋‹ค.
    // <ํƒ€์ž…ํŒŒ๋ผ๋ฏธํ„ฐ> -> <T>,<A>,<X>... ์•„๋ฌด ๊ฐ’์ด๋‚˜ ์‚ฌ์šฉ๊ฐ€๋Šฅ ํ•˜๋‹ค
    

    ์ œ๋„ค๋ฆญ ์˜ˆ์‹œ 2

    interface Mobile<T>{
        name: string;
        price: number;
        option: T;
    }
    /*
    const obj1: Mobile<object> ={
        name: '์‚ผ์„ฑ s20',
        price: 100,
        option: {
            color: 'red',
            coupon: false
        }
    };
    */
    
    const obj1: Mobile<{color:string; coupon:boolean}> ={
        name: '์‚ผ์„ฑ s20',
        price: 100,
        option: {
            color: 'red',
            coupon: false
        }
    };
    console.log(obj1.name , obj1.price +'๋งŒ์›', '์ƒ‰์ƒ:'+obj1.option.color, '์ฟ ํฐ:'+obj1.option.coupon);
    
    const obj2: Mobile<string> ={
        name: '์‚ผ์„ฑ s22',
        price: 120,
        option: 'android OS'
    };
    console.log(obj2.name , obj2.price +'๋งŒ์›', obj2.option);
    

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    ๋งˆ๋ฌด๋ฆฌํ•˜๋ฉฐ

    ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ์ดˆ๋ฅผ 1, 2ํŽธ์— ๊ฑธ์ณ์„œ ๊ณต๋ถ€ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

     

    ๋ณ€์ˆ˜ ๋ฟ๋งŒ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜๊นŒ์ง€ ๋ชจ๋“  ๊ณณ์— ํƒ€์ž…์„ ์ง€์ •ํ•ด์คŒ์œผ๋กœ์จ ์˜ค๋ฅ˜๋ฅผ ์ตœ์†Œํ™” ํ•œ๋‹ค๋Š” ๊ฒƒ์ด ๋งค๋ ฅ์ ์œผ๋กœ ๋‹ค๊ฐ€์™”์Šต๋‹ˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์„ธ์ƒ์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ๊ฒช๋Š”์ผ "์˜ค๋ฅ˜๋Š” ์•ˆ๋œจ๋Š”๋ฐ ๊ฐ’์ด ์•ˆ๋‚˜์™€์š”"๊ฐ€ ์—†๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ! ํ•œ๋ฒˆ์ฏค์€ ๊ผญ ๊ณต๋ถ€ํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๋“ฏ ์‹ถ์Šต๋‹ˆ๋‹ค.

     

    ๋Œ“๊ธ€๊ณผ ๊ณต๊ฐ์€ ํ•ญ์ƒ ํ™˜์˜์ž…๋‹ˆ๋‹ค!

    ๋Œ“๊ธ€