microsoft / microsoft/TypeScript

...args should not be readonly within function body

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

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

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

説明

Bug Report

🔎 Search Terms

readonly parameter inferred tuple array args variadic

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

Playground Link

💻 Code
declare function callFn<T extends readonly any[]>(args: T, fn: (...args: T) => void): void;

declare const input: readonly string[];

callFn(input, (...args) => {
    args; // readonly string[] ?
})

declare function callFnNonGeneric(args: readonly string[], fn: (...args: readonly string[]) => void): void;

callFnNonGeneric(input, (...args) => {
    args; // readonly string[] ?
})
🙁 Actual behavior

...args is readonly.

🙂 Expected behavior

...args is not readonly.

When this function is called, the input will be a brand new array which can be modified without affecting the caller:

> const fn = (...args) => { args[0] = "oops" }
undefined
> const arr = ["some", "values"]
undefined
> fn(...arr)
undefined
> arr
[ 'some', 'values' ]

Split out of https://github.com/microsoft/TypeScript/pull/53258#issuecomment-1470970487

The "fix" here is to strip readonly from variadic args (at the top level). This also means the fix in #53258 can be reverted (as the real fix can be more general).

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

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

はじめの一歩

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

調査の方向性

リンクされている Playground の例から始めて、readonly の可変長引数がどのように推論されるかを再現し、その後、関連する修正について PR #53258 の議論を読んでください。関数本体の rest パラメーターが変更可能でありながら、呼び出し元の readonly 配列が保持されることを確認し、この issue の例に照らして期待される動作を検証してください。

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

評価

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

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

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