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?

似ている表記法

Last updated at Posted at 2025-01-20

はじめに

TypeScriptを使う中で、幾つか似ている記法があり
自分の中での整理も兼ねてまとめました。

オブジェクト

オブジェクトとは?

オブジェクトとはキーと値のペア(プロパティ)を、1つ以上持っている
以下のようなデータ構造のことを指す。
また、JavaScriptにおいてはその表記法をオブジェクトリテラルと呼称する。

{ 
    id: 0,
    name: "Mac mini",
    cpu: "M4",
    ram: "16",
};

プロパティとは?

name: "Mac mini"

上記のような、キー(name)と値("Mac mini")のペアのことを指す

  • キーに関しては、プロパティ名とも呼称される

表記法

  • キーをクォートで囲むのは省略可能
  • 末尾のプロパティの後に、カンマを置くのは自由
  • 末尾にセミコロンを置くのは自由

JSON

JSONとは、オブジェクトリテラルをベースに作られたデータフォーマットである。
そのため、JSON自体はデータを作成するためのお約束ごとを指しており
文字列やオブジェクトのことを指しているわけではない。

{ 
    "id": 0,
    "name": "Mac mini",
    "cpu": "M4",
    "ram": "16"
}

表記法

  • キーをクォートで囲む必要がある
  • 末尾のプロパティの後に、カンマを置いてはいけない
  • 末尾にセミコロンを置いてはいけない

型エイリアス

型エイリアスとは、TypeScriptの記法を用いて名前の付けられた型のことを指す。

type Product = {
    id: number;
    name: string;
    cpu: string;
    ram: string;
};

表記法

  • 区切りにセミコロンの有無は自由
  • 代入文のため、末尾にセミコロンが必要

interface

interfaceとは、TypeScriptにおいてオブジェクトの構造を定義するための機能を指す。

interface Product {
    id: number;
    name: string;
    cpu: string;
    ram: string;
}

表記法

  • 区切りにセミコロンの有無は自由
  • 末尾にセミコロンは不要

class

(2025/01/22 追記)

class定義の表記においてもコメントをいただいたので項目として追加
コンストラクタやメソッドを利用せず、データ構造として使うパターン

class Product {
    id: number;
    name: string;
    cpu: string;
    ram: string;
}

表記法

  • 各プロパティ宣言の末尾にはセミコロンをつける
  • 末尾のプロパティに関しては、セミコロンを省略することも可能だが付けるのが大多数
  • 定義の末尾にセミコロンは不要

参考

1
0
2

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?