▶ Readonly Property : 값 수정 불가
- public readonly : 다른 누군가가 데이터를 덮어쓰는 것을 방지하면서 외부에서 값을 볼 수는 있음!
- public이지만 더이상 변경할 수 없도록 - 값은 보여주지만 수정은 불가능하게 하도록 사용
class Wrod {
constructor(
public readonly term: string,
public readonly def : string
){}
}
▶ static : 메모리할당에 이점 -> 메모리 할당을 딱 한번만 사용함!
- 객체 생성없이 클래스를 통해 메서드를 직접 호출할 수 있음!
static hello(){
return "hello";
}
▶ Type 활용
- Type 결정
- Alias
- Specific String
- Object Shape
type Health = number
//type 결정
type Nickname=string
//alias
type Team = "red" | "blue" | "yellow"
//specific string
type Player = {
nickname:Nickname,
team : Team,
Health : 1
}
//object의 모양을 결정
//이런식으로 생성
const yjh : Player={
nickname:"yjh",
team:"blue"
}
▶ Interface : object의 모양을 특정해줄 때 사용
- type과 interface의 차이 : type키워드가 interface에 비해 좀 더 활용할 수 있는게 많음
- interface는 오로지 오브젝트 모양을 타입스크립트에게 설명해 주기 위해서만 사용되는 키워드
type Team = "red" | "blue" | "yellow"
interface Player {
nickname:string,
team : Team
}
//이런식으로 생성
const yjh : Player={
nickname:"yjh",
team:"blue"
}
//아래 구문은 object가 아닌 일반 변수의 type을 설정하는 것이기 때문에 에러생성
interface hello = string
▶ Interface_Property :중첩 가능
interface User{
name:string
}
interface User{
lastName:string
}
interface User{
health:number
}
const yjh:User={
name:"yjh",
lastName:"y",
health:10
}
▶ Type vs Interface (같은 구문)
- Type
type User = {
name:string
}
type Player = User & {
}
const yjh : Player ={
name:"yjh"
}
Object뿐만이 아닌 활용도가 높음
- Interface
interface User {
name : string
}
interface Player extends User {
}
const yjh : Player ={
name:"yjh"
}
객체지향 프로그래밍과 유사함
'Web_Study > TypeScript' 카테고리의 다른 글
TypeScript_10:Summary (0) | 2022.07.01 |
---|---|
TypeScript_9:Interfaces_2 (0) | 2022.06.14 |
TypeScript_8:Class_2 (0) | 2022.06.10 |
TypeScript_7:Class (0) | 2022.06.07 |
TypeScript_6:Generic_2 (0) | 2022.06.02 |