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?

type alias とinterface 宣言

オブジェクトの型を宣言する方法には、type文を使った型エイリアス(type alias)による宣言とinterfaceを使ったinterface宣言があります。

本記事では型エイリアスとinterface宣言の差についてみていきたいと思います。

宣言時に使える構文

オブジェクトの宣言時に使える構文が型エイリアスとinterface宣言とでは異なります。

型エイリアスにしかできないこと

基本的には型エイリアスにしかできないことの方が多くあります。

まず、型エイリアスではMapped Types による型の構成が可能で、interface宣言では使用できません。

ほかにも、型エイリアスではオブジェクト型以外の型を宣言可能です。

interface宣言にしかできないこと

interface宣言でできることは型エイリアスでも代用できるものが多くありますが、Declaration Merging についてはinterface宣言特有の性質だと言えます。

型エイリアスで同名の型を宣言すると型エラーとなりますが、interface宣言の場合には型エラーとはならず、1つのinterfaceとして扱われるようになる性質があります。

エラーメッセージの種類

型エイリアスとinterfaceとでエラーメッセージの内容が異なる場合があります。

以下は、interface宣言による型のプロパティが足りないときにでる型エラーメッセージです。

interface Fish {
  name: string
}

interface Fish {
  weight: number
  height: number
}

const salmon: Fish = {
  name: "Salmon",
};
Type '{ name: string; }' is missing the following properties from type 'Fish': weight, height ts(2739)

interface宣言のときのエラーメッセージには、weightheightが無いという旨のエラーメッセージになっています。

一方で、同様のことを型エイリアスと交差型で確認してみると、エラーメッセージの種類が少し変わります。

type Name = { name: string }

type Size = {
  weight: number
  height: number
};

type Fish = Name & Size

const salmon: Fish = {
  name: "Salmon",
};
Type '{ name: string; }' is not assignable to type 'Fish'.
Type '{ name: string; }' is missing the following properties from type 'Size': weight, height ts(2322)

型エイリアスのときのエラーメッセージには、{ name: string; }Fish型の変数に代入できないという旨のメッセージが追加されており、エラーコードもinterface宣言のときとは異なっています。

コンパイラのパフォーマンスの違い

複数のオブジェクト型を合併して1つのオブジェクト型として宣言したいときに、interface宣言ではextendsを使用して表現できます。
型エイリアスの場合には、交差型&を使って同様の型を表現できます。

型エイリアスと交差型による型の宣言に比べて、interfaceextendsによって宣言する方がコンパイラのパフォーマンスが上がるという違いがあるようです。

型エイリアスでは再帰的にプロパティをマージを行い、場合によってはnever型ができてしまうのに対し、interface宣言では1つのオブジェクト型にまとめられて平坦な構造になるため、パフォーマンスが良くなるようですね。

Using interfaces with extends can often be more performant for the compiler than type aliases with intersections

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?