[Array.prototype.reduce] TS infers accumulator type as any[]

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

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

評価

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

調査の方向性

noImplicitAny を有効にした、リンク先の TypeScript Playground の再現例から始め、関連する issue 25454 および 29604 と比較します。空の配列のアキュムレーターが他の空の配列のケースと一貫して診断または報告され、期待されるコンパイラーエラーが生成されれば作業は完了です。

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

説明

In Discussion Suggestion

TypeScript Version: 3.7-Beta, "noImplicitAny" compiler mode

Search Terms:
Array.prototype.reduce, reduce type inference, reduce implicit any

Code

const result = [1].reduce((acc, item) => {
    acc.push(item)
    return acc
}, [])

result[0].toLowerCase()

Expected behavior:

  1. result implicitly has type any[] -> compiler error ("noImplicitAny")
    or
  2. result has type unknown[] -> compiler error

Actual behavior:
result has type any[], no errors

Playground Link: http://www.typescriptlang.org/play/?strictNullChecks=false&strictPropertyInitialization=false&ts=3.7-Beta&ssl=6&ssc=24&pln=1&pc=1#code/MYewdgzgLgBATgUwgVwDawLwwNoEYC6AdIgCbLAIAUlAhsMADQwCWUCAtgJQwYB8MAbwBQMUTDrBCAB2QQAFpVYdOIsYijI4YcfSEBfJtnwqhiFOmwAGIlBAAZEAHcEcAMI0IVTkA

Related Issues: https://github.com/microsoft/TypeScript/issues/25454, https://github.com/microsoft/TypeScript/issues/29604

P.S. It seems reasonable to align behavior with other empty array cases, e.g.:

let x = {
    array: [] // Object literal's property 'array' implicitly has an 'any[]' type
}

let x = []
x[0].toLowerCase() // Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

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

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

はじめの一歩

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

microsoft/TypeScript のほかの issue

microsoft/TypeScript の issue をすべて見る

似ている issue

Go の issue をもっと見る

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

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