TIL 11/7

Rami·2024년 11월 7일

TodayILearn

목록 보기
29/61

4.2 interface

type version

type User = {
    name: string
}
type Player = User & {

}
const nico : Player = {
    name: "nico"
}

interface version

interface User {
    name: string
}

interface Player extends User {]

}
const nico : Player = {
    name: "nico"
}

interface 합치기

interface User {
    name: string
}

interface User {
    firstName: string
}

interface User {
    health : number
}

const nico : User = {
    name: "nico",
    firstName: "oh",
    health : 3
}

type 합치기

=> 불가능

type User = {
    name : string
}

type User = {
    lastName: string
}

4.3 interface part two

interface 와 class의 차이점

class

abstract class User {
    constructor(
        protected firstName: string,  
        protected lastName: string
    ){}
    abstract sayHi(name: string): string
    abstract fullName(): string
}

class Player extends User {
    fullName(){
        return `${this.firstName} ${this.lastName}`
    }
    sayHi(name: string){
        return `Hello ${name}. My name is ${this.firstName}`
    }
}

interface

interface User {
    firstName: string,  
    lastName: string
    sayHi(name: string): string
    fullName(): string
}

// 추가
interface Human {
    health: number
}

class Player implements User {
    constructor(
        public firstName:string,
        public lastName: string,
        public health: number
    ){}
    fullName(){
        return `${this.firstName} ${this.lastName}`
    }
    sayHi(name: string){
        return `Hello ${name}. My name is ${this.firstName}`
    }
}

4.4 recap

type PlayerA = {
    name: string  
}

// type추가하기
type PlayerAA = PlayerA & {
    lastName: string
}

const playerA : PlayerAA = {
    name: "nico",
    lastName: "oh"
}

///////////////////////////////

interface PlayerB {
    name: string
}

// interface추가하기1
interface PlayerBB extends PlayerB {
    lastName: string
}

// interface추가하기2
interface PlayerBB {
    health: number
}

const playerB : PlayerBB = {
    name: "lynn",
    lastName: "kim",
    health: 29
}

profile
YOLO

0개의 댓글