Skip to content

Array spreads should be contextually typed #42037

Closed as not planned
Closed as not planned

Description

@mournie

TypeScript Version: 3.8

Search Terms:

  • array literal type
  • array spread type inference
  • implicit array literal type
  • array destructuring type inference [sic]

Response to Original Issue Post

We have "special" logic for unifying object literals in these cases; the spread syntax interferes with that.

If you post a more representative sample where it's not just spreading for the sake of spreading, we may be able to advise more.

Originally posted by Ryan Cavanaugh (@RyanCavanaugh) in #30761 (comment)

Real Use Case

The common pattern of literal array assignment with conditional inclusion of elements is one use case.

Code

type t = 'a'

// The common pattern
const a:t[] = [
  ...(true?['a']:[]),
]

// Minimal repro of phenomenon
const a:t[] = [
  ...['a']
]

The error messages given above are both Type 'string[]' is not assignable to type '"a"[]'.

Of course if the type of the nested array is asserted or otherwise known, the spread doesn't get in the way of that - it only seems to interrupt the automatic inference of the literal. We get as specific as string, but not as specific as the actual value.

Activity

  1. DanielRosenwasser commented on Dec 18, 2020

    @DanielRosenwasser
    Member

    I think that issue is not quite related.

    This seems pretty reasonable, though in the future please follow the template.

    const a: ("foo" | "bar")[] = [
        ...(Math.random() < 0.5 ? ["foo"] : []),
    ];

    Playground Link

  2. changed the title [-]No inference of array literal nested spread types[/-] [+]Array spreads should be contextually typed[/+] on Dec 18, 2020
  3. mournie commented on Dec 19, 2020

    @mournie
    Author

    This seems pretty reasonable, though in the future please follow the template.

    Yeah sorry - I used the "reference in new issue" button and it pre-filled the above quote instead of the standard template, so I just went with that.

  4. mournie commented on Dec 19, 2020

    @mournie
    Author

    I think that issue is not quite related.

    I guess object spread types are inferred in a way analogous to the way in which the array example isn't, so here's another example.

    type a = {one: 1}
    type b = 'b'[]
    
    const a:a = {...{one:1}} // work
    const b:b = [...['b']] // not work
    
  5. rentalhost commented on May 24, 2023

    @rentalhost

    Any updates on this case?

    type Case = [string?]
    
    const caseTrue: Case = [...(true ? ["abc" ] : [])];
    const caseFalse: Case = [...(false ? ["abc"] : [])];
  6. RyanCavanaugh commented on Oct 20, 2025

    @RyanCavanaugh
    Member

    Let's track at #45600

  7. typescript-bot commented on Oct 23, 2025

    @typescript-bot
    Contributor

    This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  8. locked as resolved and limited conversation to collaborators on Jan 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions