1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【メモ】TypeScriptのInterfaceにはアクセス修飾子のプロパティとしてreadonlyしか定義できない

Last updated at Posted at 2022-08-26

TypeScriptのInterfaceではprivateなmethodを定義できないらしいとのこと。
参考記事

下記のようにprivate methodを含むinterfaceを定義した場合にはエラーが出る。
Property is private in type 'X' but not in type 'Y' in TS

// インターフェース定義
export interface AuthServiceInterface {
  loginCustomerAdmin(email: string, password: string): Promise<TokenResponse>
  createToken(userId:string): Promise<TokenResponse> // Private methodを定義すると実装クラスでエラーが出る
}

// 実装
export class AuthService implements AuthServiceInterface {
  public async login(body: LoginCustomerAdminDto): Promise<{ token: string }> {
    return { token: '' }
  }

  private async createToken(userId: string): Promise<{ token: string }> {
    // Token生成の処理
    return { token }
  }
}

なんも知らずに愚直にやってたら1時間ぐらい時間潰れたので、同じ内容で困っている人の解決になれば幸いです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?