microsoft / microsoft/TypeScript

Member-wise checks give incorrect errors for type assertions

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

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

In Discussion Needs Investigation Rescheduled Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Playground Link

// No error. ✅
let okay = { id: 123, blah: "extra" } as { id: number };

// Errors. ❌
let waat = [{ id: 123, blah: "extra" }] as { id: number }[];
//                     ~~~~
// Conversion of type '{ id: number; blah: string; }[]' to type '{ id: number; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
//   Type '{ id: number; blah: string; }' is not comparable to type '{ id: number; }'.
//     Object literal may only specify known properties, and 'blah' does not exist in type '{ id: number; }'.

Expected: Both of these type assertions should consistently apply the appropriate relationship check, and both should be free of errors.

Actual: The first assertion is free of errors, while the second has an excess property error.

Similar example I found over at https://github.com/microsoft/TypeScript/pull/55152/files#r1275497478:

let okay = { foo: "" } as { id: 123 };
//         ~~~~~~~~~~~~~~~~~~~~~~~~~~
// Conversion of type '{ foo: string; }' to type '{ id: 123; }' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
//   Property 'id' is missing in type '{ foo: string; }' but required in type '{ id: 123; }'.

let waat = [{ foo: "" }] as { id: 123 }[];
//            ~~~
// Conversion of type '{ foo: string; }[]' to type '{ id: 123; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
//   Type '{ foo: string; }' is not comparable to type '{ id: 123; }'.
//     Object literal may only specify known properties, and 'foo' does not exist in type '{ id: 123; }'.

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground の再現から始め、PR #55152 にある類似の例も含めて、2 つの型アサーションを比較します。配列アサーションに使用されているメンバー単位の関係チェックを追跡し、報告されたケースの回帰テストを追加します。完了条件は、両方のアサーションが誤った excess-property エラーを一貫して回避することです。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

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

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