TS errors for draft objects with properties that are indexed using well-known symbols
維護者通常 4 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 新手友好度
- 48/100
- Issue 類型
- 缺陷
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- javascript, typescript
研究方向
先找出 Draft 和 WritableDraft 的型別定義,並比較它們如何處理知名符號屬性。使用 issue 中的 TypeScript repro 檢查屬性存取,以及將 draft 傳遞給 withFoo;完成的標準是 repro 在不變更預期 mutation 行為的情況下不產生 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 小時
- 30 天內合併 PR
- 22
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 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 天前認領,目前沒有進行中的 PR。 未關閉
難度 4/5 3-5 天 新手友好度 55/100
維護者通常 4 天內回覆
-
question
難度 5/5 一週以上 新手友好度 35/100
維護者通常 4 天內回覆
-
難度 5/5 一週以上 新手友好度 35/100
維護者通常 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 天內回覆