TypeScript を JavaScript から移行
既存の JavaScript プロジェクトを段階的に TypeScript に移行します。
移行戦略
- tsconfig.json を追加
- .js を .ts にリネーム
- 段階的に型アノテーションを追加
- 厳格モードを有効化
tsconfig.json の設定
tsconfig.json
{
"compilerOptions": {
// 初期段階:寛容な設定
"target": "ES2020",
"module": "commonjs",
"strict": false,
"noImplicitAny": false,
"strictNullChecks": false,
"skipLibCheck": true,
// JS ファイルを許可
"allowJs": true,
"checkJs": false,
// 出力ディレクトリ
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
"compilerOptions": {
// 初期段階:寛容な設定
"target": "ES2020",
"module": "commonjs",
"strict": false,
"noImplicitAny": false,
"strictNullChecks": false,
"skipLibCheck": true,
// JS ファイルを許可
"allowJs": true,
"checkJs": false,
// 出力ディレクトリ
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
段階的に厳密チェックを有効化
段階的に有効化
// フェーズ 1: 基本移行
{
"compilerOptions": {
"strict": false,
"noImplicitAny": false
}
}
// フェーズ 2: 型チェックを有効化
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true
}
}
// フェーズ 3: 完全に厳格
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true
}
}
{
"compilerOptions": {
"strict": false,
"noImplicitAny": false
}
}
// フェーズ 2: 型チェックを有効化
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true
}
}
// フェーズ 3: 完全に厳格
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true
}
}
JSDoc 型アノテーション
JavaScript で JSDoc を使用して型を追加します。
utils.js
/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
function add(a, b) {
return a + b;
}
/**
* @typedef {Object} User
* @property {number} id
* @property {string} name
* @property {string} email
*/
/**
* @param {number} id
* @returns {Promise<User>}
*/
function getUser(id) {
return fetch(`/api/users/${id}`).then(r => r.json());
}
* @param {number} a
* @param {number} b
* @returns {number}
*/
function add(a, b) {
return a + b;
}
/**
* @typedef {Object} User
* @property {number} id
* @property {string} name
* @property {string} email
*/
/**
* @param {number} id
* @returns {Promise<User>}
*/
function getUser(id) {
return fetch(`/api/users/${id}`).then(r => r.json());
}
実行結果:
JSDoc 注释添加成功
型宣言ファイル
型定義のないモジュールに対して宣言を作成します。
src/types/my-module.d.ts
declare module "my-module" {
export function doSomething(param: string): void;
export class MyClass {
constructor(options: { name: string });
name: string;
}
}
export function doSomething(param: string): void;
export class MyClass {
constructor(options: { name: string });
name: string;
}
}
declare キーワード
インスタンス
// グローバル変数を宣言
declare var GLOBAL_CONFIG: {
apiUrl: string;
version: string;
};
// グローバル関数を宣言
declare function myFunction(param: string): void;
// 名前空間を宣言
declare namespace MyNamespace {
function doSomething(): void;
}
// 使用
console.log(GLOBAL_CONFIG.apiUrl);
myFunction("hello");
MyNamespace.doSomething();
declare var GLOBAL_CONFIG: {
apiUrl: string;
version: string;
};
// グローバル関数を宣言
declare function myFunction(param: string): void;
// 名前空間を宣言
declare namespace MyNamespace {
function doSomething(): void;
}
// 使用
console.log(GLOBAL_CONFIG.apiUrl);
myFunction("hello");
MyNamespace.doSomething();
実行結果:
宣言成功
移行ツール
- tsc --allowJs:JS ファイルをコンパイル
- checkJs:JS の型をチェック
- // @ts-check:単一ファイルの型チェック
- // @ts-ignore:エラーを無視
legacy.js
// @ts-check
// @ts-ignore
var result = someLegacyFunction();
// @ts-ignore
var result = someLegacyFunction();
ベストプラクティス
- 主要なモジュールから移行を開始
- 単体テストを追加
- 段階のに厳格モードを有効化
- JSDoc コメントを使用
- 型宣言ファイルを作成
まとめ
- 段階的:段階的に移行
- JSDoc:型アノテーション
- 宣言ファイル:.d.ts
- 厳格モード:段階的に有効化
その他の拡張