microsoft / microsoft/TypeScript
Incorrect error attribution on indexed access, when using object spread operator
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
🔎 Search Terms
spread operator destructuring error, spread error message, object indexed access error message
🕗 Version & Regression Information
⏯ Playground Link
💻 Code
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 }
For what it's worth, this is the error from 4.2.3 and earlier:
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)
Additional information about the issue
I've tagged the issue with object spread operator because removing the spreads resolves the error. But I don't actually know whether the bug is intrinsic to spreads.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the diagnostic in the linked TypeScript Playground using versions 4.2.3, 4.3.5, and 5.7.2, then compare it with the no-spread variant. Trace the checker path that reports the indexed-access mismatch and add a regression test; done means the final diagnostic preserves the payload comparison instead of referring to EventPayloads as a whole.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100