microsoft / microsoft/TypeScript

Spread operator allows invalid return type in record data types

Ouverte
#62,095 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: check: Type Inference Help Wanted
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

### 🔎 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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par la reproduction liée dans TypeScript Playground et comparez les diagnostics avec et sans l’entrée sans spread. Étudiez comment le résultat du spread est vérifié par rapport à FnRecord ; le travail est terminé lorsque le type de retour string invalide produit l’erreur de mismatch attendue, tandis que les valeurs de record valides restent acceptées.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.