#4.0 Classes
TS๋ก ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ ํ๊ธฐ
Copy class Player {
constructor (
private firstName: string, // JS ์๋ private๊ฐ ์์ TS์์๋ง ์๋
private lastName: string,
public nickname: string
) {}
}
const nic = new Player("nic", "las", "n")
nic.firstname // TS์์์๋ ์ํจ private ์ด๊ธฐ์
nic.nicname // ์๋
Abstract - ๋ค๋ฅธ ํด๋์ค๊ฐ ์์๋ฐ์ ์ ์๋ ํด๋์ค, ์ง์ ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ ์๋ ์์
JS์๋ ์๋ํ์ง ์์ Only TS
Copy abstract class User { // ์ถ์ ํด๋์ค
constructor (
private firstName: string,
private lastName: string,
public nickname: string
) {}
abstract getNickName(): void // absc- ์์ absc ๊ฐ๋ฅ, class์์ ๋ฐ๋์ ๊ตฌํํด์ผํจ
getFullName(){ // ์ถ์ ๋ฉ์๋
return `${this.firstName} ${this.lastName}`
}
}
class Player extends User {
...
}
const nic = new Player("nic", "las", "n")
nic.getFullName() // "nic las"
์ ๊ทผ ๊ฐ๋ฅ ์์น
๊ตฌ๋ถ
์ ์ธํ ํด๋์ค ๋ด
์์๋ฐ์ ํด๋์ค ๋ด
์ธ์คํด์ค
=> TS์์ ์ฒดํฌํ๊ณ JS์์๋ ๋ณด์ด์ง ์์
#4.1 Recap
4.0 ๋ณต์ต
Copy type Words = {
[key: string] : string // Words ํ์
์ด string๋ง์ ํ๋กํผํฐ๋ก ๊ฐ๋ ์ค๋ธ์ ํธ
}
class Dic {
private words: Words
constructor() {
this.words = {}
}
add(word: Word){ // ํด๋์ค๋ฅผ ํ์
์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค
if(this.words[word.term] === undefined){
this.words[word.term] = word.def;
}
}
def(tern: string){
return this.words[term]
}
}
class Word {
constructor() {
public term: string,
public def: string
} {}
}
const kimchi = new Word("kimchi", "ํ๊ตญ์์")
const dic = new Dic(kimchi)
dic.add(kimchi)
dic.def("kimchi")
+ mission
#4.2 Interfaces
ํ์
๊ณผ ๋น์ทํ์ง๋ง ๋ค๋ฆ
Copy // ํ์
// ํน์ ๊ฐ๋ง ๊ฐ์ง๋๋ก ์ ํ ๊ฐ๋ฅ
type Team = "MU" | "BM" | "NA"
type Health = 1 | 5 | 10
type Player = {
nickname: string,
team: Team,
health: Health,
}
const kim : Player = {
nickname: Kim,
team: BM,
health: 11 // err : ์ค์ ๋ ํ์
์ด ์๋๊ธฐ์ ์๋ฌ ๋ฐ์
}
Interface - ์ค์ง ํ๊ฐ์ง ์ฉ๋๋ก ์ค๋ธ์ ํธ์ ๋ชจ์์ ํน์ ํ๊ธฐ ์ํด ์ฌ์ฉ
Copy // ์ธํฐํ์ด์ค
type Team = "MU" | "BM" | "NA"
type Health = 1 | 5 | 10
interface Player {
nickname: string,
team: Team,
health: Health,
}
const kim : Player = {
nickname: Kim,
team: BM,
health: 11 // err : ์ค์ ๋ ํ์
์ด ์๋๊ธฐ์ ์๋ฌ ๋ฐ์
}
type๊ณผ interface๋ ์ค๋ธ์ ํธ์ ๋ชจ์์ ํน์ ํ๊ธฐ ์ํ ์ฉ๋์ด๋ฏ๋ก ๊ฑฐ์ ๊ฐ์
Copy interface User {
name: string
}
interface Player extends User {}
const kim : Player = {
name: "Kim"
}
/////////
type User = {
name: string
}
type Player = User & {}
const kim : Player = {
name: "Kim"
}
interface๋ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ๊ฐ๋
์ ํ์ฉํด ๋์์ธ๋์๊ณ , ํ์
์ ๋ ์ ์ฐํ๊ณ ๊ฐ๋ฐฉ์ ์
Copy // Interface ํ๋กํผํฐ์ ์ถ์ ์ด ๊ฐ๋ฅํจ
interface User {name: string}
interface User {lastName: string}
interface User {age: number}
const nico :User = {
name: "nico",
lastName: "n",
age: 10
}
#4.3 Interfaces part Two
์ธํฐํ์ด์ค๋ ๊ฐ๋ณ๋ค. ์ปดํ์ผํ๋ฉด JS๋ก ๋ณํ์ง ์๊ณ ์ฌ๋ผ์ง๊ธฐ ๋๋ฌธ์ด๋ค.
Copy 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.fullName()}`
}
}
// => abstract ๋ฅผ interface ๋ก ๋ณ๊ฒฝ
interface User {
firstName: string,
lastName: string,
sayHi (name: string):string
fullName (): string
}
class Player implements User { // implements ๋ JS ์ ์ฌ์ฉ๋์ง ์์
constructor(
public firstName: string,
public lastName: string
){}
fullName(){
return `${this.firstName} ${this.lastName}`
}
sayHi(name:string){
return `Hello ${name}. My Name is ${this.fullName()}`
}
}
์ ๋ ์ฝ๋๋ ๊ฐ์ ๋ด์ฉ์ abstract๋ก ์์ฑํ์ ๋์ interface๋ก ์์ฑํ์ ๋์ ์ฐจ์ด๋ค. ์ด๋ ๊ฒ ๋ณด๋ฉด ํฐ ์ฐจ์ด ์์ด๋ณด์ด์ง๋ง, JS๋ก ์ปดํ์ผํ ๋ด์ฉ์ ์ดํด๋ณด๋ฉด ๋ถํ์ํ User class๊ฐ ์ฌ๋ผ์ ธ์์์ ํ์ธํ ์ ์๋ค. ์ด๋งํผ ๋ ๊ฐ๋ฒผ์์ก๋ค๋ ์๋ฏธ์ด๋ค.
#4.4 Recap
Type๊ณผ Interface ๋ ์ค๋ธ์ ํธ์ ๋ชจ์์ ์ง์ ํ๋ ๋์ผํ ๊ธฐ๋ฅ์ ์ํํ๋ค.
Copy type PlayerA = {
name: string
}
const playerA: PlayerA = {
name: "Kim MJ"
}
///
interface BlayerB {
name: string
}
const playerB: PlayerB = {
name: "Son HM"
}
์ถ๊ฐ์ ์ธ ๋ถ๋ถ์์ ์ฐจ์ด๊ฐ ์๊ธด๋ค.
Copy type PlayerA = {
name: string
}
type PlayerAA = PlayerA & {
lastName: string
}
const playerA: PlayerAA = {
name: "MJ"
lastName: "Kim"
}
///
interface PlayerB {
name: string
}
interface PlayerBB extends PlayerB { // ์ด๋ฐ ์์ผ๋ก ์์ ๊ฐ๋ฅํ๋ค.
lastName: string
}
interface PlayerBB { // ์ด๋ฐ ์์ผ๋ก ํ์ฅ ๊ฐ๋ฅํ๋ค.
health: 10
}
const playerB: PlayerB = {
name: "HM"
lastName: "Son"
health: 10
}
์ถ์ class๋ฅผ ๋์ฒดํ๋ค.
Copy type PlayerA = {
firstName: string
}
class User implements PlayerA {
constructor(
public firstName: string
){}
}
interface PlayerB {
firstName: string
}
class User implements PlayerB {
constructor(
public firstName: string
){}
}
=> ํ์
์คํฌ๋ฆฝํธ์ ์ค๋ธ์ ํธ์ ๋ชจ์์ ์๋ ค์ฃผ๊ณ ์ถ์ผ๋ฉด interface, ๋๋จธ์ง ์ํฉ์์๋ type์ ์ฌ์ฉํด๋ผ~
#4.5 Polymorphism
๋คํ์ฑ - ๋ค๋ฅธ ๋ชจ์์ ์ฝ๋๋ฅผ ๊ฐ์ง ์ ์๊ฒ ํ๋๊ฒ. ์ ๋ค๋ฆญ(placeholder ํ์
)์ ์ฌ์ฉํ๋ค.
Copy interface SStorage<T> {
[key: string]: T
}
class LocalStoage<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 stringsStroage = new LocalStorageMstring<string>()
stringsStorage.get("ee")
stringsStorage.set("hello", "world")
stringsStorage.set("hello", 1) // err T is string
const booleansStorage = new LocalStorage<boolean>()
booleansStorage.get("xxx")
stringsStorage.set("hello", "world")