0
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?

【TypeScript初心者メモ】type型

Last updated at Posted at 2025-06-07

typeとは

typeは、型に名前(別名)をつける仕組みです。
基本構文:type 型名 = 型定義;

具体例

1.基本型のエイリアス

// UserId は number 型の別名。
type UserId = number;
// 意味づけを明確にできる(例:IDとして使う数値だとわかる)
const id: UserId = 123;

2.オブジェクト型のエイリアス

// よく使う構造に名前をつけて再利用できる
type User = {
  id: number;
  name: string;
  isActive: boolean;
};
const user1: User = {
  id: 1,
  name: "Taro",
  isActive: true
};

3.関数型のエイリアス

// 関数の型を表現して再利用
type Greet = (name: string) => string;
const greetUser: Greet = (name) => `Hello, ${name}`;

4.配列・タプルの型エイリアス

type NameList = string[];
const names: NameList = ["Taro", "Hanako"];
type Point = [number, number];
const pos: Point = [10, 20];
0
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
0
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?