Angular After Tutorialで下記のコードがありますが、これの.get<{ data: User[] }>
の<>
はジェネリックの型引数ですが、普通は<string>
や<インターフェース型>
などを指定しますが、<{ data: User[] }>
はどういう意味でしょうか?
型引数にオブジェクトでkey: valueの型を入れているということでしょうか?
typescript
import { Component } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { User } from './user'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { users: User[] = []; constructor(private http: HttpClient) {} ngOnInit() { this.http .get<{ data: User[] }>('https://reqres.in/api/users') .subscribe((rsep) => { this.users = rsep.data; }); } }
このようなジェネリックの使い方はTypeScriptの学習の中で見たことがないので、参考サイトなどあれば教えて頂きたいです。
よろしくお願いします。
参考
https://zenn.dev/lacolaco/books/angular-after-tutorial/viewer/first-refactoring
まだ回答がついていません
会員登録して回答してみよう