0
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】Utility Types

Posted at

Partial<T>

Tの全てのプロパティをOptionalにする。

type Profile = {
  name: string
  age: number
  zipCode: number
}

type PartialType = Partial<Profile>
// type PartialType = {
//   name?: string | undefined;
//   age?: number | undefined;
//   zipCode?: number | undefined;
// }

Required<T>

Tの全てのプロパティを全て必須にする。

type Profile = {
  name: string
  age?: number
  zipCode?: number
}

type RequiredType = Required<Profile>
// type RequiredType = {
//   name: string;
//   age: number;
//   zipCode: number;
// }

Readonly<T>

Tのプロパティを全てreadonlyにする。

type Profile = {
  name: string
  age: number
}

type PersonalDataType = Readonly<Profile>

const friend: PersonalDataType = {
  name: "田中",
  age: 19
}

// friend.age++
// error TS2540: Cannot assign to 'age' because it is a read-only property.

type Yomitorisenyo<T> = {
  readonly [P in keyof T]: T[P];
};
type YomitorisenyoProfile = Yomitorisenyo<Profile>
// type YomitorisenyoProfile = {
//   readonly name: string;
//   readonly age: number;
// }

Record<K, T>

Record<K, T>KがプロパティのT型を持つレコード型を構築する。

type Prefectures = "Tokyo" | "Chiba" | "Kyoto"

type DemographicsInfo = {
  kanji_name: string
  population: number
}

const Demographics : Record<Prefectures, DemographicsInfo> = {
  Tokyo: { kanji_name: "東京", population: 13515 },
  Chiba: { kanji_name: "千葉", population: 6223 },
  Kyoto: { kanji_name: "京都", population: 2610 },
}

Exclude<T,U>

TのプロパティでUと互換性のあるプロパティを除いた型を構築する。

// Exclude<ユニオン型, 除外する型>
type DebugType = () => void
type SomeTypes = string | number | DebugType
type FunctionType = Exclude<SomeTypes, string | number>
type NonFunctionType = Exclude<SomeTypes, DebugType>
// 関数を除外する場合
type TypeExcludingFunction = Exclude<SomeTypes, Function>

// type Exclude<T, U> = T extends U ? never : T;
// type MyExclude = 
// ( string extends string | number ? never : string )  // never
// | ( number extends string | number ? never : number )  // never
// | ( DebugType extends string | number ? never : DebugType )  // DebugType
// type MyFunctionType = MyExclude

Extract<T,U>

Tの中からUと互換性のあるプロパティのみ残した型を構築する。

// Extract<ユニオン型, 抽出する型>
type FunctionTypeByExtract = Extract<SomeTypes, DebugType>
type NonFunctionTypeByExtract = Extract<SomeTypes, string | number>
type TypeExcludingFunctionByExtract = Extract<SomeTypes, Function>

NonNullable<T>

Tからnullundefinedを除いた型を構築する。

// nullとundefinedを除外する
type NullableTypes = string | number | null | undefined
type NonNullableTypes = NonNullable<NullableTypes>

Pick<T,K>

Tの中からKで選択したプロパティのみを含んだ型を構築する。

type DetailedProfile = {
  name: string
  height: number
  weight: number
}

type SimpleProfile = Pick<DetailedProfile, "name" | "weight">
//  type SimpleProfile = {
//      name: string;
//      weight: number;
//  }

type Pick<T, K extends keyof T> = {
  [P in K]: T[P];
};

Omit<T,K>

Tの中からKで選択したのプロパティを除いた型を構築する。

type DetailedProfile = {
  name: string
  height: number
  weight: number
}

type SmallProfile = Omit<DetailedProfile, "height">
// type SmallProfile = {
//   name: string;
//   weight: number;
// }

// type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
// K = height
// keyof T = "name" | "height" | "weight"
// Exclude<keyof T, K> = Exclude<"name" | "height" | "weight", "height"> = "name", "weight"
// type MyOmit = Pick<DetailedProfile, 'name' | 'weight'>;
// type MySmallProfile = MyOmit
0
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
0
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?