let's get IT with DAVINA ๐Ÿ’ป

TypeScript(Interface) ๋ณธ๋ฌธ

DEV_IN/TypeScript

TypeScript(Interface)

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 16. 19:37

๋“ค์–ด๊ฐ€๊ธฐ ์ „ ํ•˜๋‚˜ ์•Œ๊ณ  ๊ฐ€์ž +1

Static 

  • JS method์ž…๋‹ˆ๋‹ค.
  • method๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, ์›๋ž˜๋Š” new Hi์™€ ๊ฐ™์€ instance๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•ด์„œ hello()๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•˜๋Š”๋ฐ
  • static ์ •์  ๋ฉ”์†Œ๋“œ๋ฅผ ์“ฐ๋ฉด, ๋ฐ”๋กœ class์—์„œ method ํ˜ธ์ถœ ๊ฐ€๋Šฅ!
class Hi {
	constructor() {
		static hello() {
			return "hello";
		}
	}
}

Hi.hello(); //ok to go

Type VS Interface

  • ๋‘ ๊ฐœ์˜ ๊ณตํ†ต ๋ชฉํ‘œ?
    • object์˜ ๋ชจ์–‘๊ณผ ํƒ€์ž…์„ ์„ค๋ช…ํ•ด์ค๋‹ˆ๋‹ค.

Type์˜ ๊ธฐ๋Šฅ

  • Type (Object)
    • Shape of object (๊ฐ์ฒด๊ฐ€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”๊ฐ€?)๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • Property Name์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -> nickname, age
    • Property Type์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -> string, number
type Player = {
	nickname:string,
	age: number
}

const vini : Player = {
	nickname: "vini",
	age: 28
}
  • Type (String)
    • type์ด object๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ํƒ€์ž…์ž„์„ (string์ž„์„) ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
type Food = string;

const kimchi : Food = "delicious"
  • Type (aliases)
    • type์„ ์ง€์ •ํ•ด์„œ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
type Nickname=string
type Age=number

type Player = {
	nickname:Nickname,
	age: Age
}
  • Type (Specific value)
    • Type๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํŠน์ •ํ•˜๊ฒŒ ์ง€์ •๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
type Team= "red"|"blue"|"yellow"

type Player = {
	nickname: string,
	team:Team
}

const vini:Player={
	nickname:"vini",
	team:"yellow" //"pink"๋Š” ๋ถˆ๊ฐ€๋Šฅ!
}
์ด ๋•Œ, Object์˜ ๋ชจ์–‘๊ณผ ํƒ€์ž…์„ ์ง€์ •ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด Type๋ง๊ณ  ์“ธ ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€?
โ–ท Interface!!

Interface

  • only 1๊ฐœ์˜ ๊ธฐ๋Šฅ๋งŒ ํ•จ!
    • type์ด ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
    • Object์˜ shape & type์„ explainํ•ด์ค๋‹ˆ๋‹ค.

type๊ณผ ๋น„๊ตํ•ด์„œ, interface๋ฅผ ์“ธ ๋•Œ๋Š” "="์„ ๋นผ์ฃผ๊ณ  ์‚ฌ์šฉํ•ด์š”

type Player = {
	nickname: string,
	team:Team
}

interface Player {
	nickname: string,
	team:Team
}

class ์˜ ์“ฐ์ž„์ƒˆ์™€ ๋งค์šฐ ์œ ์‚ฌํ•˜๋‹ค (๊ธฐ๋Šฅ๋„ ๋น„์Šท)

  • ์ƒ์† ๋ฐฉ๋ฒ•์ด type๊ณผ ๋‹ค๋ฆ„
  • ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ฒ˜๋Ÿผ ๋ณด์ผ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ
interface User {
	name:string
}

interface Player extends User {
}

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

์œ„์˜ ์˜ˆ์‹œ๊ฐ€ type์ด์—ˆ๋‹ค๋ฉด?

type User = {
	name:string
}

type Player = User & { //&์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•ด์•ผํ•จ
}

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

property accumulate์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ property๋ฅผ ์ถ”ํ›„์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
interface User {
	name:string
}

interface User {
	lastName: string
}

interface User {
	age:number
}

const vini:User={
	name:"vini",
	lastName: "An",
	age: 28
}

์œ„์˜ ์˜ˆ์‹œ๊ฐ€ type์ด์—ˆ๋‹ค๋ฉด?

type User = {
	name:string
}

type User = {
	lastName: string //โ›”๏ธ User์„ ๊ณ„์† ์ •์˜ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ž‘๋™์•ˆ๋ผ!!! ํ•œ๋ฒˆ์— ์ž‘์„ฑํ•ด์ค˜์•ผ๋ผ!!
}

Interface VS class

class 

abstract class๋ฅผ ์“ฐ๋Š” ์ด์œ ?
โ–ท ์ƒ์†๋ฐ›๋Š” class๋“ค์ด ์–ด๋–ค method๋ฅผ ๊ตฌํ˜„ํ•˜๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ฃผ์ง€,
์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š”์ง€๋Š” abstract class์—์„œ ์ด๋ฏธ ๋‹ค ํ•ด์ฃผ๋‹ˆ๊นŒ!
abstract class User { //abstract class=> JS์—์„  class User๋กœ ์ผ๋ฐ˜์ ์ธ ํด๋ž˜์Šค๋กœ ๋ฐ”๋€œ
    constructor(
        protected firstName:string, //protected=> ์ถ”์ƒํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋“ค์ด property์— ์ ‘๊ทผํ•˜๋„๋ก ํ•ด์คŒ
        protected lastName:string
    ) {}
    abstract sayHi(name:string):string
    abstract fullName():string
}

class Player extends User{
    fullName() {
        return `${this.firstName} ${this.lastName}`
    }
    sayHi(name: string) { //abstract class๋Š” ์–ด๋–ค method๋ฅผ implementํ•ด์•ผ๋˜๋Š”์ง€๋ฅผ ์•Œ๋ ค์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, type์„ ํ•œ๋ฒˆ ๋” ์ง€์ •ํ•ด์ค˜์•ผํ•จ
        return `Hello ${name}. My name is ${this.fullName()}`
    }
}

Interface

  • ๋‚ด๊ฐ€ ์›ํ•˜๋Š” method์™€ property๋ฅผ class๊ฐ€ ๊ฐ•์ œ๋กœ ๊ฐ€์ง€๋„๋ก ํ•˜๊ฒŒ ํ•ด์คŒ
  • JS์—์„  interface๊ฐ€ ์—†์Œ! TS์—์„œ๋งŒ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—, JS ์ฝ”๋“œ์—์„  ๋ณด์ด์ง€๊ฐ€ ์•Š์Œ
  • JS code๋ฅผ smaller, lighterํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ค„ ์ˆ˜ ์žˆ์–ด์ง
interface User{ 
    firstName: string,
    lastName: string,
    sayHi(name:string):string
    fullName():string
}

interface Human{
    health: number
}
//์œ„์˜ interface๋“ค์€ JS์—์„œ ์•ˆ๋ณด์ž„

//implements=JS์—๋Š” ์—†๋Š” ๋ฉ”์†Œ๋“œ๋ผ class Player{}๋กœ ๋‚˜์˜ค๊ฒŒ ๋จ
class Player implements User,Human {
    constructor(
        public firstName: string, //๋ฌธ์ œ์ ์ด ์žˆ๋‹ค๋ฉด.. private&protected๋Š” ์•ˆ๋จ!
        public lastName: string,
        public health: number
    ){}
    fullName() {
        return `${this.firstName} ${this.lastName}`;
    }
    sayHi(name: string) {
        return `Hello ${name}. My name is ${this.fullName()}`
    }
}
type PlayerA = {
    firstName: string
}

interface PlayerB{
    firstName: string
}

//type & interface ๋‘˜ ๋‹ค implement ๊ฐ€๋Šฅ
class User implements PlayerA{
    constructor(
        public firstName: string
    ){}
}
โญ๏ธ object์˜ ๋ชจ์–‘๊ณผ ํƒ€์ž…์„ ์ •์˜ํ•˜๊ณ  ์‹ถ์œผ๋ฉด interface๋ฅผ ์“ฐ๊ณ , ์ด์™ธ์— ๊ฒฝ์šฐ์—” ๋‹ค type์„ ์“ฐ๋ฉด ๋ผ!

Polymorphism

  • ๋‹คํ˜•์„ฑ => ๋‹ค์–‘ํ•œ ๋ชจ์–‘์˜ ์ฝ”๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค
  • generics๋ฅผ ์ด์šฉํ•˜๋ฉด ๋˜๊ฒ ์ฐŒ?!!?!?
    • not a concrete type, it is a placeholder type
interface SStorage<T> {
    [key:string]: T
}

class LocalStorage<T> {
    private storage : SStorage<T> = {}
    set(key:string, value: T){
        this.storage[key]=value;
    }
    remove(key:string){
        delete this.storage[key]
    }
    get(key:string):T{
        return this.storage[key]
    }
    clear(){
        this.storage={}
    }
}

const stringsStorage=new LocalStorage<string>()
stringsStorage.get("ket")
stringsStorage.set("hello", "how are you") //generics๋ฅผ string์œผ๋กœ ์ง€์ •ํ•ด์คฌ๊ธฐ ๋•Œ๋ฌธ์—, string์œผ๋กœ value๋ฅผ ์ง€์ •ํ•˜๋ผ๊ณ  ํ•จ

const booleanStorage=new LocalStorage<boolean>();
booleanStorage.get("XXX")
booleanStorage.set("hello", true) //generics๋ฅผ boolean์œผ๋กœ ์ง€์ •ํ•ด์คฌ๊ธฐ ๋•Œ๋ฌธ์—, boolean type์œผ๋กœ value๋ฅผ ์ง€์ •ํ•˜๋ผ๊ณ  ํ•จ

'DEV_IN > TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

โ›”๏ธ ์ž์ฃผ ๋ณผ ์ˆ˜ ์žˆ๋Š” Errors  (0) 2023.02.17
TypeScript(apply)  (2) 2023.02.17
TypeScript(Classes)  (4) 2023.02.12
TypeScript (Function)  (3) 2023.02.09
TypeScript๋ž€? & Types  (0) 2023.02.09
Comments