microsoft / microsoft/TypeScript

Warn/error when testing a non-nullable type for null

オープン
#10,477 コメント 11 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

TypeScript Version: 2.0.0

TL;DR:

If a project is configured for strict null checks, checking a variable which is typed as non-null is nonsense and could indicate a type mismatch.

Code:

Here's a motivating example I experienced:

async function someFunc1(): Promise<string | null> {
  // do some stuff and return the appropriately typed promise
}

async function someFunc2(): Promise<boolean> {
  // Negating a non-null Promise is crazy talk, but here I go!
  return !!someFunc1()
}

Of course what I really meant was:

async function someFunc2(): Promise<boolean> {
  return !!(await someFunc1())
}

Expected behavior:

TypeScript warns that I'm effectively testing my non-null Promise for null, and points out that that's nonsense.

Actual behavior:

TypeScript happily accepted the code, which led to a bug.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue にはファイル、テスト、エントリーポイントが指定されていません。まず、例で説明されている strict-null-checking の動作を確認し、TypeScript が non-nullable な式に対する null テストをどのように処理するかを追跡してください。不正なチェックによって診断が生成され、valid な nullable チェックが拒否されなければ完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。