microsoft / microsoft/TypeScript

Contextual type doesn't apply to elements of array literal spread into another array literal

Open
#45,600 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Contextual Types Effort: Moderate Help Wanted
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

spread operator, type inference

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

Playground link with relevant code

💻 Code
interface Test {
    field: 'a' | 'b' | 'c'
}

let arr: Test[] = [
    { field: 'c' },
    ...[
        { field: 'a' }, // field: string
//        ^?
        { field: 'b' }, // field: string
//        ^?
    ]
]

function test(...args: Test[]): void {}

test(
    { field: 'c' },
    ...[
        { field: 'a' }, // field: string
//        ^?
        { field: 'b' }, // field: string
//        ^?
    ]
)

Workbench Repro

🙁 Actual behavior

The types of objects in the array nested through the spread operator is not inferred to be Test, as such inference begins from scratch, so the type of field in those objects becomes string (and is not detected to be 'a' | 'b' | 'c' which I specified in the interface), and a rather cryptic type error is shown:

Type '{ field: string; }' is not assignable to type 'Test'.
  Types of property 'field' are incompatible.
    Type 'string' is not assignable to type '"a" | "b" | "c"'.

I have also included an example with using the spread operator in a function call for completeness, it generates basically the same error.

I'm not sure if this is really a bug, but regardless, it is trivial to work around by just forcing compiler's hand a little by doing this:

let arr: Test[] = [
    { field: 'c' },
    ...[
        { field: 'a' as const },
        { field: 'b' as const },
    ]
]

Which forces the inferred types of the objects to actually be compatible, or assigning the spread array to a variable with an explicitly defined type:

let nested: Test[] = [
    { field: 'a' },
    { field: 'b' },
]
let arr: Test[] = [
    { field: 'c' },
    ...nested
]
🙂 Expected behavior

I would expect the type of expr3 in [expr1, expr2, ...expr3] to be inferred to be Iterable<T> or something like that provided that we know that the type of elements of the overall array is T. I believe that in my repro it would make types of nested objects to be inferred correctly as Test.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Playground repro and confirm the contextual typing behavior for array literals containing spreads, then compare it with the Workbench repro for the function-call case. Done means the nested object elements are inferred as compatible with Test without the shown as const or separately typed-array workarounds, with both examples behaving as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.