TS errors for draft objects with properties that are indexed using well-known symbols
メンテナーはふだん 4 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 48/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- javascript, typescript
調査の方向性
まず Draft と WritableDraft の型定義を見つけ、既知のシンボルプロパティをどのように扱っているかを比較します。issue の TypeScript repro を使用して、プロパティアクセスと draft を withFoo に渡す処理の両方を確認します。意図したミューテーション動作を変更せずに、repro で TS エラーが発生しなければ完了です。
索引モデルが issue の本文から書いたものです。
説明
🐛 Bug Report
Draft<T> objects (at least according to TS) don't bring along any symbol-indexed properties from the original object. Is this expected? I'm not sure if this is only an issue in the TS types, or is the underlying JS draft object also missing its symbol-indexed properties?
Although well-known symbols are usually used with classes, I was able to repro the problem with a plain object too.
I'm using TS 4.1, if it matters.
FWIW, I ran into this because I was trying to call produce() with a deeply-nested object which had a Uint8Array-valued property far down in the tree. When I tried to pass the draft object to another method that expected the original type, TS complained that [Symbol.iterator] and [Symbol.toStringTag] properties that were required on Uint8Array but missing on the Draft<T> object. I'm not trying to mutate the array (that'd be #696); I'm just expecting a deep clone but (at least from TS's perspective) the symbol properties are missing.
Link to repro
import produce, { Draft } from 'immer';
const foo = {
a() { return 42; },
get [Symbol.toStringTag]() { return 'foo'; }
}
type Foo = typeof foo;
function withFoo(f: Foo) {
return f[Symbol.toStringTag];
}
const ok = foo[Symbol.toStringTag]; // no error
const ok2 = withFoo(foo); // no error
const producer = produce((draft: Draft<Foo>) => {
draft.a(); // no error
withFoo(draft);
/* ^^^^^
Argument of type 'WritableDraft<{ a(): number; readonly [Symbol.toStringTag]: string; }>'
is not assignable to parameter of type '{ a(): number; readonly [Symbol.toStringTag]: string; }'.
Property '[Symbol.toStringTag]' is missing in type 'WritableDraft<{ a(): number;
readonly [Symbol.toStringTag]: string; }>' but required in type
'{ a(): number; readonly [Symbol.toStringTag]: string; }'. (2345)
*/
const fail = draft[Symbol.toStringTag];
/* ^^^^^^^^^^^^^^^^^^^^^^^^^
Element implicitly has an 'any' type because expression of type 'symbol' can't be used
to index type 'WritableDraft<{ a(): number; readonly [Symbol.toStringTag]: string; }>'.(7053)
*/
});
LMK if you want me to turn the TS playground code above into a CodeSandbox example; happy to do so.
To Reproduce
Steps to reproduce the behavior:
- Create a producer that accepts a TS type that has a well-known symbol property
- Try to read that property or call a function that accepts a parameter of
TfromDraft<T>. Note that mutation is not required-- this problem affects read-only access.
Observed behavior
TS errors as noted above.
Expected behavior
No TS errors.
Environment
We only accept bug reports against the latest Immer version.
- Immer version:
- I filed this report against the latest version of Immer
- [n/a (TS only)] Occurs with
setUseProxies(true) - [n/a (TS only)] Occurs with
setUseProxies(false)(ES5 only)
- 主要言語
- JavaScript
- スター
- 29k
- フォーク
- 905
- 平均マージ
- 3日 16時間
- マージ済み PR(30日)
- 22
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
immerjs/immer のほかの issue
-
Shrinking an array with `length =` and growing it back loses the patches for the truncated indices対応中かも @RClaudiuM が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 4 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
メンテナーはふだん 4 日以内に返信
-
Docs: Chinese (zh-CN) website translation is outdated vs English docs再び着手できるかも @lijiayou0728 が 91 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 55/100
immerjs/immer#1242 · コメント 5 件 ·
メンテナーはふだん 4 日以内に返信
-
question
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
メンテナーはふだん 4 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
immerjs/immer#1217 · コメント 1 件 ·
メンテナーはふだん 4 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
Automattic/node-canvas#2633 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
capricorn86/happy-dom#2594 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
microsoft/monaco-editor#5575 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
メンテナーはふだん 1 日以内に返信