Skip to content

Reimplement InstanceType<T> #1084

@ryota-murakami

Description

@ryota-murakami

Reimplement InstanceType<T>

TypeScript built-in Utility Type を自前で再実装し、テストを記述して理解を深める。

Original Definition (src/lib/es5.d.ts)

/**
 * Obtain the return type of a constructor function type
 */
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;

学習ポイント

  • コンストラクタの 戻り値 = インスタンスの型
  • ReturnType<T> のコンストラクタ版
  • ConstructorParameters<T> との対の関係

やること

  • packages/types/ に再実装ファイルを作成
  • Vitest の expectTypeOf を使ったテストを記述
  • 通常のクラス、abstract クラス、ビルトインクラス(Date, Error 等)のケースをカバー

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions