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?

More than 1 year has passed since last update.

インターフェースとは何か

Posted at

TypeScriptのインターフェースを理解するために備忘録として残します。

インターフェースとは何か

インターフェースとはオブジェクトがどんな形のものか定義するためのもの
JavaScriptには存在しない

インターフェースを使用するには

  • interfaceキーワードを使用する
  • 名前の先頭は大文字にする
  • メソッドも追加できる
app.ts
interface Person {
    name: string;
    age: number;
    greet(phrase: string): void;
}

インターフェースを使用するメリット

  • オブジェクトの型チェックに使える
app.ts
interface Person {
    name: string;
    age: number;
    greet(phrase: string): void;
}

let userOne: Person;
user1 = {
    name: 'Mary';
    age: 40;
    greet(phrase: string) {
        console.log(phrase);
    }
}

userOne.greet('Hello');
1
0
1

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?