General class

class Player {
		constructor(
				private firstName: string,
				private lastName: string,
				public nickname: string
		) {}
}

const nico = new Player("nico", "las", "니꼬");

Abstract class

abstract class User {
		constructor(    // Call signature of the method
				private firstName: string,
				private lastName: string,
				protected nickname: string
		) {}
		abstract getNickName(): void
}

class Player extends User {
		getNickName() {
				console.log(this.nickname) // ✅
		}
}

const nico = new Player("nico", "las", "니꼬");

nico.nickname // ❌ beacause protected

Example

type Words = {
		[key: string]: string
}

class Dict {
		private words: Words  // Put the words without the initializer.
		constructor() {       // Then in the constructor,
				this.words = {}   // we are going to initiallize the words manually.
		}
		add(word: Word) {  // Use a class as a type
				if(this.words[word.term] === undefined){
						this.words[word.term] = word.def
				}
		}
		def(term: string) {
				return this.words[term]
		}
}

class Word {
		constructor(
				public readonly term: string,
				public readonly def: string
		) {}
}
// 😡 term과 def 두개의 property를 public으로 선언했기 때문에
// kimchi.def = "xxxx" 등으로 수정가능, readonly를 사용해야함.

const kimchi = new Word("kimchi", "Korean food");
const dict = new Dict()

dict.add(kimchi);
dict.def("kimchi");

Interface

if you want to specify the shape of an object to typescript, you have two options.

Interface is only used to specify the shape of the objects. they only work for one thing.

but Type is more flexible.

type Team = "red" | "blue" | "yellow"
type Health = 1 | 5 | 10 ⭕
// interface Hello = string ❌

interface Player {
		nickname: string,
		team: Team,
		health: Health
} // ⭐Shape of object
/*
type Player = {
		nickname: string,
		team: Team,
		health: Health
} // ⭐Shape of object
*/

const nico: Player = {
		nickname: "nico"
		team: "yellow"
		health: 10
}

Differences between classes and interfaces

But javascript doesn’t have the concept of the abstract 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.fullName()`
		}
}

Interface is only on the typescript, it doesn’t compile to the javascript.

🚩The problem with implementing interfaces is the fact that you cannot have a private properties.

⭐and interface cannot have the constructor functions.