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?

More than 3 years have passed since last update.

Intersection 型をinterfaceで同じような型を作ろうと思ったらエラーがでた

Last updated at Posted at 2021-05-08

初めに

この記事はTypeScriptを学ぶにあたっての備忘録です。

気付き

下記はtypeを使った問題のない例です。


type Hoge = {
  name: string;
  age: number;
}

type Fuga = {
  name: string;
  size: number;
}

type EngineerBlogger = Hoge & Fuga

const valiable: HogeFuga = {
  name: "hoge",
  age: 1,
  size: 1
}

こちらをinterfaceでやるとエラーが出る。


interface Hoge {
  name: "hoge"
  role: string;

}
interface Fuga {
  name: "fuga"
  follower: number;
}

//インターフェイス 'HogeFuga' で型 'Hoge' と型 'Fuga' を同時には拡張できません。 'Hoge' 型および 'Fuga' 型の名前付きプロパティ 'name' が一致しません。
interface HogeFuga extends Hoge, Fuga {} 

結論

なるほどです。。同じことをやっていると思ったんですがそういうわけではないのかと思った次第。
typeでやっていることはIntersection 型なのであくまでも 『交差』 で 今回interfaceでやったのは拡張だから同じnameを使うことができないということなのか。。多分。

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?