microsoft / microsoft/TypeScript

'Canvas#getContext' almost never returns 'null'

オープン
#19,229 コメント 14 件 リアクション 3 件 担当者 0 名 GitHub で見る

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

Awaiting More Feedback Domain: lib.d.ts Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

TypeScript Version: 2.5.3

Code:

let canvas: HTMLCanvasElement = document.createElement("canvas");
let ctx: CanvasRenderingContext2D = canvas.getContext("2d");
ctx.fillRect(0, 0, canvas.width, canvas.height);

Expected behavior:
No errors should occur because the type of canvas is HTMLCanvasElement.

Actual behavior:

test.ts(2,5): error TS2322: Type 'CanvasRenderingContext2D | null' is not assignable to type 'CanvasRenderingContext2D'.
  Type 'null' is not assignable to type 'CanvasRenderingContext2D'.

If I change the type of ctx to CanvasRenderingContext2D | null, then I get

test.ts(3,1): error TS2531: Object is possibly 'null'.

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

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

はじめの一歩

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

調査の方向性

まず、HTMLCanvasElement.getContext("2d")を使用して報告された TypeScript 2.5.3 のスニペットを再現し、宣言された戻り値の型を、issue に記載されている Canvas API の動作と比較します。CanvasRenderingContext2D に対して意図されている nullability を判断し、そのうえで、例が型チェックを通過し、fillRect の呼び出しによって nullability エラーが発生しないことを確認します。

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

評価

技術スタック
typescript
領域
frontend, web-dev
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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