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.

Typescriptのジェネリックを理解する

Posted at

ジェネリック

//ジェネリックを使うと、外部からデータ型を指定できるので、クラスや関数をいくつも作る必要がなくなる。
class Store<T> {
  //T(Type),U,V,T1,T2,T3などジェネリックを指定するのが慣例となっている。
  data: T; //インスタンス化する際に、<string>が渡されているので、データ型がstringになる。
  getsStore(): T {
    return this.data;
  }
}

let stringData = new Store<string>(); //インスタンス化する際に、クラス名の後ろに<string>のように渡す。

let booleanData = new Store<boolean>();

//ジェネリックを複数
class Component<T,U>{
  name: T;
  create: U;
}
let component = new Component<string,number>();
component.created = '2021/05/08'; 
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?