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?

TypeScript ~備忘録~

Last updated at Posted at 2024-10-21

TSの記法を見返す用

私が学んだTSの記法を記します。ほかにも便利な記法、これは知っとけがあったら教えてください。

TSの特徴

  1. JSに変換されてから実行(TSはブラウザ上で実行できない,tscongi.jsonファイルに変換設定が書いてある。)
  2. 型の敵が可能
  3. JSにない記述が使用可能

メリット

 型適宜によるチーム開発の円滑化
公開用ライブラリへの型定義
バグの事前検知
VSCodeの自動補間

デメリット

型を定義しなければならない
JavaScript特有の柔軟で簡易な記述の損失

TSを使う方法

npx create-react-app {プロジェクト名} --template typescript

※コンポーネントを定義するときはtsx

プリミティブな値の型定義

let str: string = "hello"

ユニオン型

let strOrNum: string | number = "hello";
strOrNum = 12;

型推論(type inderence)

let str = "hello";

※あまり使わない。使うのであれば関数の中でのみ使う。

ジェネリクス

型引数を受け取る関数を作る機能のこと

const repeat = <T>(value: T, times: number): T[] => {
    return new Array(times).fill(value);
};
//numberは省略可能
const num2Array = repeat<number>(10, 7);
console.log(num2Array);
0
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
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?