1
1

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 の strict 設定、最適な順序とは?

1
Posted at

TypeScript の strict オプションは、コードの安全性を高めるために便利ですが、一度に全部有効にするとエラーが大量に出て、初心者はつまずきやすいです。そこで「段階的に strict を有効にする」手順と、設定ファイル (tsconfig.json) に書くべき順序を解説します。

1. まずは noImplicitAny だけ有効にする

any が暗黙的に許容されると、型チェックの恩恵が薄れます。最初のステップとして次のように設定します。

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "strict": false,
    "noImplicitAny": true
  }
}

この段階で「暗黙の any がある」箇所だけがエラーになるので、具体的に型を付与すべき場所が見えてきます。

2. strictNullChecks を追加

次に null と undefined の扱いを厳密にします。strictNullChecks を有効にすると、null や undefined が予期しない場所で使われたときに警告が出ます。

{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true
  }
}

ここまでで、変数が必ず値を持つことが保証されるようになり、実装時のバグが減ります。

3. strictFunctionTypes と strictBindCallApply を順に有効化

関数の型チェックを強化します。特にコールバックやイベントハンドラを書き始めたら有効にしたいオプションです。

{
  "compilerOptions": {
    "strictFunctionTypes": true,
    "strictBindCallApply": true
  }
}

この段階で、引数の型が不一致だった場合にエラーが出るので、API の呼び出し側と実装側の契約が明確になります。

4. noImplicitThis と alwaysStrict をオン

this の暗黙的な型や、ESモジュールの厳格モードを有効にします。

{
  "compilerOptions": {
    "noImplicitThis": true,
    "alwaysStrict": true
  }
}

noImplicitThis はクラスやオブジェクトリテラル内部で this が期待通りに型付けされることを保証し、alwaysStrict は JavaScript の「use strict」相当の振る舞いをデフォルトで適用します。

5. 最後に strict フラグでまとめて有効化

上記のオプションをすべて確認したら、strict フラグを true にして一括で有効化できます。これにより、将来的に新しい strict 系オプションが追加されたときも自動で有効になります。

{
  "compilerOptions": {
    "strict": true
  }
}

まとめ

  1. noImplicitAny → 2. strictNullChecks → 3. strictFunctionTypes / strictBindCallApply → 4. noImplicitThis / alwaysStrict → 5. strict の順に有効化すると、エラーの増加に戸惑わず段階的に型安全性を高められます。
    初心者でも無理なく TypeScript の厳格モードに慣れられるので、ぜひ自分のプロジェクトで試してみてください。

詳しい手順はこちら → https://felixstudio0.gumroad.com/?utm_source=qiita&utm_medium=github_actions&utm_campaign=2026-q1&utm_content=qiita-footer

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?