microsoft / microsoft/TypeScript
Type modifier in mapped types break `any`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Bug Report
🔎 Search Terms
mapped types, any, contravariance, type modifiers
🕗 Version & Regression Information
4.3.2
⏯ Playground Link
(the playground seems down for me, but I'll update the ticket when I can)
💻 Code
// Adding/removing `-?` makes the `anyCallback` line flip between broken/working
type Mapped<T> = { [K in keyof T]-?: number };
type Callback<T> = (object: Mapped<T>) => void;
const fn: Callback<{ firstName: string }> = () => {};
const anyCallback: Callback<any> = fn;
🙁 Actual behavior
The anyCallback = fn line causes an error:
TS2322: Type 'Callback<{ firstName: string; }>' is not assignable to type 'Callback<any>'.
Types of parameters 'object' and 'object' are incompatible.
Property 'firstName' is missing in type 'Mapped<any>' but required in type 'Mapped<{ firstName: string; }>'.
🙂 Expected behavior
I expect to be able to assign Callback<{ firstName: string} to Callback<any> because { firstName: string } should be considered any.
If I remove the -? type modifier in the Mapped type, then this works as expected.
Which leads me to the naive assertion that "the type modifier breaks any".
Note it's only the required type modifier -?, the required modifier +? doesn't cause the compile error.
Guida per i contributori
Apri la guida per i contributori
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.
Direzione di ricerca
Inizia riproducendo l'esempio di TypeScript 4.3.2 con il tipo mappato, il modificatore -? e l'assegnazione di Callback<any>. Analizza la gestione dei tipi mappati e dell'assegnabilità da parte del compilatore; il lavoro è completato quando l'assegnazione segnalata si comporta come previsto senza regredire nel comportamento differente dei modificatori di tipo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100