본문 바로가기
Language/TypeScript

클래스

by Zayne 2023. 2. 21.

■ 클래스

 

class A {
  private a: string;
  #b: number = 123; // # = private 으로 클래스 내부에서만 사용 가능
  // 생성자
  constructor(a: string) {   // b 에는 기본 값 할당
    this.a = a;
  }
}

// class 는 타입이 될 수 있음
type B = A;

const a: A = new A("123");
const b: typeof A = A;

 

■ 인터페이스

 

interface A {
  readonly a: string;
  b: string;
}

class B implements A {
  a: string = "hello";
  b: string = "world";
  private c: string = "private";
  protected d: string = "protected";
}

class C extends B {
  public method() {
    console.log(this.a);
    console.log(this.b);
    console.log(this.c); // private 변수로 에러 발생
    console.log(this.d);
  }
}
new C().a;
new C().b;
new C().c;  // private 변수로 에러 발생
new C().d;  // pretected 변수로 에러 발생

'Language > TypeScript' 카테고리의 다른 글

기본값  (0) 2023.02.25
Optional, Generic  (0) 2023.02.23
readonly, index Signature, Mapped Types  (0) 2023.02.20
{} , Object  (0) 2023.02.19
커스텀 타입 가드  (0) 2023.02.18