3
1

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 で配列の中身の型を取得する Unwrap 型

Posted at

コード

type Unwrap<T> = T extends { [K in keyof T]: infer U } ? U : never

使い方

type HogeList = {
  id: number
  name: string
}[]

type FugaList = {
  id: number
  label: string
}[]

type Result = Unwrap<HogeList | FugaList> // ココ

Result はこう解釈される

type Result =
  | {
      id: number
      name: string
    }
  | {
      id: number
      label: string
    }

自動生成された型に含まれる配列を解体したい時とかに便利。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?