🔷 TypeScriptチートシート
| 構文 | 用途 | 例 |
|---|---|---|
string / number / boolean | 基本型 | let name: string = "太郎"; |
type | 型エイリアス | type ID = string | number; |
interface | オブジェクトの型定義 | interface User { name: string; age: number; } |
? | オプショナルプロパティ | interface User { email?: string; } |
readonly | 読み取り専用 | readonly id: number; |
union (|) | 複数の型のいずれか | let value: string | number; |
intersection (&) | 型の合成 | type Admin = User & Permissions; |
literal type | 特定の値のみ許可 | type Dir = "up" | "down" | "left" | "right"; |
generics | 汎用的な型パラメータ | function first |
as | 型アサーション | const el = document.getElementById("x") as HTMLInputElement; |
typeof | 値から型を取得 | type Config = typeof defaultConfig; |
keyof | オブジェクトのキーの型 | type Keys = keyof User; // "name" | "age" |
enum | 列挙型 | enum Color { Red, Green, Blue } |
tuple | 固定長配列 | let pair: [string, number] = ["age", 25]; |
never | 到達しない型 | function error(msg: string): never { throw new Error(msg); } |
unknown | 安全なany | let data: unknown; // 使う前に型チェック必須 |
Partial | 全プロパティをオプショナルに | Partial |
Required | 全プロパティを必須に | Required |
Pick | 特定のプロパティだけ抽出 | Pick |
Omit | 特定のプロパティを除外 | Omit |
Record | キーと値の型を指定した辞書 | Record |
type guard | 型の絞り込み | if (typeof x === "string") { x.toUpperCase(); } |
satisfies | 型チェックしつつ推論を保持 | const config = { port: 3000 } satisfies Config; |
infer | 条件型で型を推論 | type Return |
declare | 型だけ宣言(実装なし) | declare module "*.css"; |
📖 関連する用語
❓ よくある質問
TypeScriptチートシートはどんな人向け?
TypeScript初心者〜中級者向け。基本の型からユーティリティ型まで網羅しています。
印刷して使える?
はい。印刷に最適化されたレイアウトで出力されます。
JavaScriptとの違いは?
TypeScriptはJavaScriptに型を追加した言語です。このチートシートはTS固有の構文をまとめています。
⚠️ よくあるエラー
- Object is possibly undefined — null チェック不足
- Property does not exist on type — 存在しないプロパティ