TypeScript 08 クラス・モジュール・型定義ファイル
更新日:
クラス
class Counter {
#count = 0 // JS 標準のプライベート(実行時も隠れる)
private step: number // TS の private(型チェック時だけ。実行時は見える)
readonly name: string
constructor(name: string, step = 1) {
this.name = name
this.step = step
}
increment(): number { return (this.#count += this.step) }
}
interface Repository<T> { find(id: string): Promise<T | null> }
class UserRepository implements Repository<User> { // 契約を満たすか検査
async find(id: string) { return null }
}
abstract class Shape { abstract area(): number } // 継承専用- 修飾子:
public(既定)/protected(サブクラスから)/private/readonly/static - パラメータプロパティ
constructor(private repo: Repo) {}は短く書けるが、JS に変換が必要な構文なのでerasableSyntaxOnly環境では使えない - 実務の React / Next.js ではクラスより関数とオブジェクトが中心。クラスはエラー型(
class NotFoundError extends Error)やSDKで見かける程度
モジュール(import / export)
// user.ts
export type User = { id: string; name: string }
export function createUser(name: string): User { /* ... */ }
export default function main() {}
// app.ts
import main, { createUser, type User } from './user'
import type { Config } from './config' // 型だけの import(実行時に消える)import typeを使うと「型のためだけの import」が明確になり、ビルドツールが安全に削除できる。verbatimModuleSyntax: trueにすると型 import にtypeを付けることが必須になる(推奨)- ファイルに import / export が1つもないと「スクリプト」扱いで、変数がグローバルになる
- パスの別名:
#/components/...(package.json のimports、TS 6 で#/形式に対応)や tsconfig のpaths(@/*など)
型定義ファイル(.d.ts)
型の情報だけを書いたファイル。JS で書かれたライブラリに型を付けるために使う。
// types/global.d.ts
declare global {
interface Window { dataLayer: unknown[] }
}
export {}- ライブラリの型の入手先:
- パッケージ自身が同梱(最近はほとんどこれ。
package.jsonのtypes/exports) - DefinitelyTyped:
npm i -D @types/パッケージ名(例:@types/node、@types/react) - どちらも無い →
declare module 'lib-name'で自作(最低限any相当になる)
- パッケージ自身が同梱(最近はほとんどこれ。
- ⚠️ TS 6 から
typesの既定値が[]に。@types/nodeなどを自動で読み込まなくなったため、必要なものは tsconfig の"types": ["node"]に明示する(TypeScript_10_tsconfig・strict・実行環境・バージョン)
宣言のマージ・モジュール拡張
ライブラリの型に項目を足したい時(例: 環境変数、認証ライブラリのセッション型)。
// env.d.ts(Vite の環境変数に型を付ける)
interface ImportMetaEnv { readonly VITE_API_URL: string }
interface ImportMeta { readonly env: ImportMetaEnv }
// Node の process.env
declare namespace NodeJS {
interface ProcessEnv { DATABASE_URL: string }
}- 関連: Vite_02_開発の基本(TS・CSS・静的ファイル・環境変数)、Cloudflare は
wrangler typesでEnvの型を生成(Cloudflare)
JS ファイルを型チェックする
allowJs+checkJs、または JS ファイル先頭に// @ts-check- JSDoc で型を書ける:
/** @param {string} name */ - 段階的な移行:
allowJsで混在させ、1ファイルずつ.tsへ