객체지향 언어에서의 TS
class Player {
constructor(
private firstName:string,
private lastName:string,
public nickname:string
) {}
}
const nico = new Player("nico", "las", "니꼬");
nico.firstName
nico.nickname
abstract class User {
constructor(
protected firstName:string,
protected lastName:string,
protected nickname:string
) {}
abstract getNickname():void
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
class Player extends User {
getNickname() {
console.log(this.nickname)
}
}
const nico = new Player("nico", "las", "니꼬");
nico.getFullName()
interface
type Team = "red" | "blue" | "yellow"
interface Player {
nickname:string,
team:Team
}
const nico : Player = {
nickname: "nico",
team: "red"
}
interface User {
name:string
}
interface Player extends User {
health:number
}
const nico : Player = {
name: "nico",
health: 10
}
interface로 class 만들기
interface User {
firstName:string,
lastName:string,
sayHi(name:string):string,
fullName():string,
}
interface Human {
health:number
}
class Player implements User, Human {
constructor(
public firstName:string,
public lastName:string,
){}
fullName() {
return `${this.firstName} ${this.lastName}`
}
sayHi(name:string) {
return `Hello ${name}. My name is ${this.fullName()}`
}
}
interface를 type으로 쓰기
interface User {
firstName:string,
lastName:string,
sayHi(name:string):string,
fullName():string,
}
function makeUser(user: User): User {
return {
firstName: "nico",
lastName: "las",
fullName: () => "nicolas",
sayHi: (name) => "string"
}
}
makeUser({
firstName: "nico",
lastName: "las",
fullName: () => "nicolas",
sayHi: (name) => "string"
})