TS errors for draft objects with properties that are indexed using well-known symbols
I maintainer di solito rispondono entro 4 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 48/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- javascript, typescript
- Ambito
- developer-experience, tooling
Direzione di ricerca
Inizia individuando le definizioni dei tipi di Draft e WritableDraft e confronta il modo in cui gestiscono le proprietà dei simboli noti. Usa il repro TypeScript nell’issue per verificare sia l’accesso alle proprietà sia il passaggio di un draft a withFoo; il lavoro è completato quando il repro non produce errori TS senza modificare il comportamento di mutazione previsto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
🐛 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)
- Lingua principale
- JavaScript
- Stelle
- 29k
- Fork
- 905
- Merge medio
- 3g 16h
- PR unite (30g)
- 22
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di immerjs/immer
-
Shrinking an array with `length =` and growing it back loses the patches for the truncated indicesForse già presa @RClaudiuM l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 4 giorni
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
I maintainer di solito rispondono entro 4 giorni
-
Docs: Chinese (zh-CN) website translation is outdated vs English docsForse di nuovo libera @lijiayou0728 l’ha presa 91 giorni fa e non c’è nessuna pull request aperta. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 55/100
immerjs/immer#1242 · 5 commenti ·
I maintainer di solito rispondono entro 4 giorni
-
question
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
I maintainer di solito rispondono entro 4 giorni
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
immerjs/immer#1217 · 1 commento ·
I maintainer di solito rispondono entro 4 giorni
Tutte le issue di immerjs/immer
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
Automattic/node-canvas#2633 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
capricorn86/happy-dom#2594 ·
I maintainer di solito rispondono entro 1 giorno
-
Decoding tag 1 (epoch date) with an 8-byte integer throws "Cannot mix BigInt and other types"Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
microsoft/monaco-editor#5575 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno