microsoft / microsoft/TypeScript

nested destructured tuple value is `string | undefined` when second tuple item is not accessed

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

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

Bug Domain: flag: noUncheckedIndexedAccess
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

When using noUncheckedIndexedAccess, the type inferred by my code is affected by whether I access the second item in a tuple or not.

🔎 Search Terms

tuple, undefined, type, omit, noUncheckedIndexedAccess, error, index, array, infer, nested

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about noUncheckedIndexedAccess
  • I was unable to test this on prior versions because --noUncheckedIndexedAccess didn't exist before those versions
⏯ Playground Link

Playground link with relevant code

💻 Code
declare const fn: <T extends string[]>(t: T) => [T, string[]];

const [[x]] = fn(['1']);

console.log(x.toLowerCase());

It seems that the inferred type for fn is:

const fn: <string[]>(t: string[]) => [string[], string[]]

Since the first item is an array rather than a tuple, items in that array have type string | undefined.

If I change the assignment to be:

const [[x], []] = fn(['1']);

It gets inferred as:

const fn: <[string]>(t: [string]) => [[string], string[]]

Since the first item is a tuple rather than an array, items in that tuple have type string.

🙁 Actual behavior

x has type string | undefined

🙂 Expected behavior

x has type string.

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

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

はじめの一歩

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

調査の方向性

リンク先の TypeScript Playground から始め、noUncheckedIndexedAccess の下で 2 つの分割代入形式を再現します。レポートに表示される推論された型の違いを追跡します。1 つ目の形式で x の型が string になり、2 つ目の形式では期待される動作が維持されれば完了です。

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

評価

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

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

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