Repository: TypeScript
Issue: Incorrect error attribution on indexed access, when using object spread operator
Labels: Help Wanted,Possible Improvement

### 🔎 Search Terms

`spread operator destructuring error`, `spread error message`, `object indexed access error message`

### 🕗 Version & Regression Information

- This changed between versions [4.2.3](https://www.typescriptlang.org/play/?ts=4.2.3#code/C4TwDgpgBAogbhAdsACgQxAGwPZoCYDOUAvFAN4BQU1UAxtgLZiYTAQDKYATgJbIBc5KjRFoArsEZpgPbIgCSeQQWC9EAcwA0wkdQJg0tCIuWq+WnVAC+2kQSR4AshAIE06iIMq6aDF249TNQsRGworCgpQSCgAYUZmVgg8eCRgAB4AFSgIAA82REIoAGsIEGwAM1gEZHQsXEIAPhIhEQBtGCg+KEyAXS9LEWjPWFsfKAN6-EFU2owcfAIO3rGfNDAwADk0PyDzVetwtr7IirFEWhk5KGwELgB3XjYAQQ3tvyyc-IciUvKq2aoeYNAiNAAUlgItAAFskxCwlHEEiw2CkahlMo1tABKQTxJgo5KArLNbw0eiIFTkKDrLY7CCaKAAOhZXBcwGsLShsLw8OSlgA9AKoAB1CA8Lh4HJcLjYLhQPyudzQWFsxkqNBcGQaLqIKAAFiZAGZLGzgGIuHqyT4WUy2SoDqI3vTBAAiNgqV0HCJWIA) and [4.3.5](https://www.typescriptlang.org/play/?ts=4.3.5#code/C4TwDgpgBAogbhAdsACgQxAGwPZoCYDOUAvFAN4BQU1UAxtgLZiYTAQDKYATgJbIBc5KjRFoArsEZpgPbIgCSeQQWC9EAcwA0wkdQJg0tCIuWq+WnVAC+2kQSR4AshAIE06iIMq6aDF249TNQsRGworCgpQSCgAYUZmVgg8eCRgAB4AFSgIAA82REIoAGsIEGwAM1gEZHQsXEIAPhIhEQBtGCg+KEyAXS9LEWjPWFsfKAN6-EFU2owcfAIO3rGfNDAwADk0PyDzVetwtr7IirFEWhk5KGwELgB3XjYAQQ3tvyyc-IciUvKq2aoeYNAiNAAUlgItAAFskxCwlHEEiw2CkahlMo1tABKQTxJgo5KArLNbw0eiIFTkKDrLY7CCaKAAOhZXBcwGsLShsLw8OSlgA9AKoAB1CA8Lh4HJcLjYLhQPyudzQWFsxkqNBcGQaLqIKAAFiZAGZLGzgGIuHqyT4WUy2SoDqI3vTBAAiNgqV0HCJWIA)

### ⏯ Playground Link

[Playground link, 5.7.2](https://www.typescriptlang.org/play/?ts=5.7.2#code/C4TwDgpgBAogbhAdsACgQxAGwPZoCYDOUAvFAN4BQU1UAxtgLZiYTAQDKYATgJbIBc5KjRFoArsEZpgPbIgCSeQQWC9EAcwA0wkdQJg0tCIuWq+WnVAC+2kQSR4AshAIE06iIMq6aDF249TNQsRGworCgpQSCgAYUZmVgg8eCRgAB4AFSgIAA82REIoAGsIEGwAM1gEZHQsXEIAPhIhEQBtGCg+KEyAXS9LEWjPWFsfKAN6-EFU2owcfAIO3rGfNDAwADk0PyDzVetwtr7IirFEWhk5KGwELgB3XjYAQQ3tvyyc-IciUvKq2aoeYNAiNAAUlgItAAFskxCwlHEEiw2CkahlMo1tABKQTxJgo5KArLNbw0eiIFTkKDrLY7CCaKAAOhZXBcwGsLShsLw8OSlgA9AKoAB1CA8Lh4HJcLjYLhQPyudzQWFsxkqNBcGQaLqIKAAFiZAGZLGzgGIuHqyT4WUy2SoDqI3vTBAAiNgqV0HCJWIA)

### 💻 Code

```ts
type EventPayloads = {
    completeSprint: {
        automationId: string,
        spaceId: string,
    },
    sendMessage: {
        message: string,
    },
}

type CompletedEvent<T extends keyof EventPayloads> = {
    [E in T]: {
        type: E,
        payload: EventPayloads[E],
        appName: string,
    }
}[T]

function overwriteAppName<T extends keyof EventPayloads>(
    scheduled: CompletedEvent<T>,
): CompletedEvent<T> {
    const { appName, ...rest } = scheduled
    // Weird error message here, starting in 4.3
    return {
        ...rest,
        appName: "test",
    }
}
```


### 🙁 Actual behavior

The `return` statement has the following error:
```
Type 'Omit<CompletedEvent<T>, "appName"> & { appName: string; }' is not assignable to type 'CompletedEvent<T>'.
  Types of property 'payload' are incompatible.
    Type 'CompletedEvent<T>["payload"]' is not assignable to type 'EventPayloads[T]'.
      Type '{ type: T; payload: EventPayloads[T]; appName: string; }' is missing the following properties from type 'EventPayloads': completeSprint, sendMessage(2322)
```
The first three lines make sense, but the last line is bizarre enough that I suspect it's a bug in either the reporting/checking logic.

Given that `EventPayloads` and `CompletedEvent` are completely different objects, this error made me think I had missed an indexing step somewhere, but after rereading the code multiple times I'm pretty sure I didn't (it seems like a type error in the type error).

Also, my best possible explanation for the reduction in this step
```
    Type 'CompletedEvent<T>["payload"]' is not assignable to type 'EventPayloads[T]'.
      Type '{ type: T; payload: EventPayloads[T]; appName: string; }' is missing the following properties from type 'EventPayloads': completeSprint, sendMessage(2322)
```
is that the algorithm eliminated the indexed access on both sides, so that

| | `CompletedEvent<T>["payload"]` | `EventPayloads[T]` |
|-|-|-|
|becomes | `CompletedEvent<T>` | `EventPayloads` |

but this is clearly incorrect considering `T != "payload"`.

### 🙂 Expected behavior

Given the penultimate line of the error, I would expect the final line to compare payload types, i.e. `{ automationId: string, spaceId: string }` vs. `{ message: string }`

<details>
<summary>
For what it's worth, this is the error from <a href="https://www.typescriptlang.org/play/?ts=4.2.3#code/C4TwDgpgBAogbhAdsACgQxAGwPZoCYDOUAvFAN4BQU1UAxtgLZiYTAQDKYATgJbIBc5KjRFoArsEZpgPbIgCSeQQWC9EAcwA0wkdQJg0tCIuWq+WnVAC+2kQSR4AshAIE06iIMq6aDF249TNQsRGworCgpQSCgAYUZmVgg8eCRgAB4AFSgIAA82REIoAGsIEGwAM1gEZHQsXEIAPhIhEQBtGCg+KEyAXS9LEWjPWFsfKAN6-EFU2owcfAIO3rGfNDAwADk0PyDzVetwtr7IirFEWhk5KGwELgB3XjYAQQ3tvyyc-IciUvKq2aoeYNAiNAAUlgItAAFskxCwlHEEiw2CkahlMo1tABKQTxJgo5KArLNbw0eiIFTkKDrLY7CCaKAAOhZXBcwGsLShsLw8OSlgA9AKoAB1CA8Lh4HJcLjYLhQPyudzQWFsxkqNBcGQaLqIKAAFiZAGZLGzgGIuHqyT4WUy2SoDqI3vTBAAiNgqV0HCJWIA">4.2.3</a> and earlier:
</summary>

```
Type 'Omit<CompletedEvent<T>, "appName"> & { appName: string; }' is not assignable to type 'CompletedEvent<T>'.
  Types of property 'payload' are incompatible.
    Type 'CompletedEvent<T>["payload"]' is not assignable to type 'EventPayloads[T]'.
      Type '{ type: T; payload: EventPayloads[T]; appName: string; }' is missing the following properties from type 'EventPayloads': completeSprint, sendMessage
        Type 'CompletedEvent<T>["payload"]' is not assignable to type '{ automationId: string; spaceId: string; } & { message: string; }'.
          Type 'EventPayloads[T]' is not assignable to type '{ automationId: string; spaceId: string; } & { message: string; }'.
            Type '{ automationId: string; spaceId: string; } | { message: string; }' is not assignable to type '{ automationId: string; spaceId: string; } & { message: string; }'.
              Type '{ automationId: string; spaceId: string; }' is not assignable to type '{ automationId: string; spaceId: string; } & { message: string; }'.
                Property 'message' is missing in type '{ automationId: string; spaceId: string; }' but required in type '{ message: string; }'.
                  Type 'EventPayloads[T]' is not assignable to type '{ automationId: string; spaceId: string; }'.
                    Type 'CompletedEvent<T>["payload"]' is not assignable to type '{ automationId: string; spaceId: string; }'.
                      Type 'EventPayloads[T]' is not assignable to type '{ automationId: string; spaceId: string; }'.
                        Type '{ automationId: string; spaceId: string; } | { message: string; }' is not assignable to type '{ automationId: string; spaceId: string; }'.
                          Type '{ message: string; }' is missing the following properties from type '{ automationId: string; spaceId: string; }': automationId, spaceId(2322)
```
</details>

### Additional information about the issue

I've tagged the issue with `object spread operator` because [removing the spreads resolves the error](https://www.typescriptlang.org/play/?ts=5.7.2#code/C4TwDgpgBAogbhAdsACgQxAGwPZoCYDOUAvFAN4BQU1UAxtgLZiYTAQDKYATgJbIBc5KjRFoArsEZpgPbIgCSeQQWC9EAcwA0wkdQJg0tCIuWq+WnVAC+2kQSR4AshAIE06iIMq6aDF249TNQsRGworCgpQSCgAYUZmVgg8eCRgAB4AFSgIAA82REIoAGsIEGwAM1gEZHQsXEIAPhIhEQBtGCg+KEyAXS9LEWjPWFsfKAN6-EFU2owcfAIO3rGfNDAwADk0PyDzVetwtr7IirFEWhk5KGwELgB3XjYAQQ3tvyyc-IciUvKq2aoeYNAiNAAUlgItAAFskxCwlHEEiw2CkahlMo1tABKQTxJgo5KArLNbw0eiIFTkKDDTQTYH4awtKGwvDw5KWAD0nKgAHUeMBodgJFB9FwIPg6YLoBAuFxsFwoOpsC4oGh7hgAHSWcXAMRcRCtca0wY0SYLPAHURvHYjABEbBUdoOESsQA). But I don't actually know whether the bug is intrinsic to spreads.