Skip to content

Literals that are spread into a const rest are not const-ified #53177

Closed

Description

Bug Report

🔎 Search Terms

spread rest literal expression string array const modifier

🕗 Version & Regression Information

  • This is the behavior in every version I tried (only relevant to 5.0+)

⏯ Playground Link

Playground link with relevant code

💻 Code

declare function foo<const T extends readonly unknown[]>(...args: T): T;

const res1 = foo(...[1, 'bar']);
//    ^? readonly [number, string]
const res2 = foo(..."hello");
//    ^? readonly string[]

🙁 Actual behavior

When spreading an array expression its elements are not acting like they are in the const context. When spreading a string it's not even inferred as a tuple of strings.

🙂 Expected behavior

I'm not exactly sure what is the expected result here - I can see the argument going both ways. I just think that perhaps this is a little bit underspecified today and that this issue, at the very least, might help to specify the desired behavior for those cases.

Activity

  1. RyanCavanaugh commented on Mar 9, 2023

    @RyanCavanaugh
    Member
  2. ahejlsberg commented on Mar 9, 2023

    @ahejlsberg
    Member

    This is working as intended. Spread elements intentionally don't have contextual types, so the const-ness isn't propagated to the tuple type. It would add a good deal of complexity to compute contextual types for spread expressions, and in many cases it just isn't possible. Furthermore, in real world code, spread expressions are pretty much never literals as that's just a more inconvenient way of writing the elements directly inline.

  3. Andarist commented on Mar 9, 2023

    @Andarist
    ContributorAuthor

    Thanks for the answer! It’s pretty much what i expected but was a little but unsure about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions