microsoft / microsoft/TypeScript

Spread operator allows invalid return type in record data types

Aperta
#62,095 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Type Inference Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 Search Terms

spread operator, return type mismatch on record of functions

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries. Nothing related was mentioned.
I've tried 3.3, 4.0, 5.0 and 5.8.3 and got the same result.

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.3#code/C4TwDgpgBAYgdgHgJIBooHkB8UC8UDeUokAXFAEQBmc5A3FNWQBQCWcYArsGUgJS7Z0UAL60AUGOLR4AJQgBjAPYAnACbI0WXFDlK1CAM7BlbAOZp4GjNgA+UDnFURKbCKszixlB-OAtFcESKsgoq6qjWTGJQUACGvv5wBmS6YYbGZmis7Fw8-DiCUHYOTi5wbphivGQheuGa2PjRRAAWyooA7lDlXQCiyu3KTLziwhJKScBENXCp+gBuiiyqaHAcALYARhDK2HhNMbFkhFJkVDRojFDDAlAATCJoAPRPUAgAtFBK6+sQcH5wUxQRRcVosAxQAA2rigyggBg4kOAELYcUCO0GUFRwBa0Gh5Sg20hnWaADpycBgrNQmomIRNsx8thyABmcgiXgoMSiMRAA

### 💻 Code

```ts
type Fn = { type: "fn"; fn: (input: I) => O };

type FnRecord = Record | undefined>;

function toFnRecord(
actions: Record O | undefined>
): FnRecord {
throw new Error();
}

const t: FnRecord = {
a: { type: "fn", fn: () => 2 }, // <- commenting out this line results in an error in the line below
...toFnRecord({ b: () => "3" }),
}
```

### 🙁 Actual behavior

No error was reported

### 🙂 Expected behavior

Expect the following type mismatch error message. This is reported if I **comment out** the entry without spread operator (`a: { type: "fn", fn: () => 2 },`)
```
Type '{ [x: string]: Fn | undefined; }' is not assignable to type 'FnRecord'.
'string' index signatures are incompatible.
Type 'Fn | undefined' is not assignable to type 'Fn | undefined'.
Type 'Fn' is not assignable to type 'Fn'.
Type 'string' is not assignable to type 'number'.(2322)
```

### Additional information about the issue

Similar to #61754, but this example does not involve any explicit generic subtyping constraints (`extends`).

I'm not sure if this is related to #10727.

I'd like to know any workaround that can prevent this type error happening at runtime. I've run into this multiple times with the same pattern used in my codebase and got very surprised each time.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione collegata in TypeScript Playground e confronta le diagnosi con e senza l’elemento senza spread. Esamina come il risultato dello spread viene verificato rispetto a FnRecord; il lavoro è completato quando il tipo di ritorno string non valido produce l’errore di mismatch previsto, mentre i valori di record validi continuano a essere accettati.

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
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.