Skip to content

Expected tuple type as function return value is not infered from written code #53911

Closed

Description

Hello TypeScript team !

Bug Report

I have an issue when expecting a tuple as a function return value and trying to return a tuple without explicitly specifiying that it is a tuple.
TypeScript assumes that I am returning any Array, although the syntax of the code is that of a tuple.

I have the following function:

(value) => [value.some((element) => (element))] // NOK

The type of value is : (parameter) value: [boolean, boolean, boolean, boolean, boolean, boolean]
The expected return type is [boolean]

With the above function, I get the following type error from TypeScript:

Argument of type '(value: [boolean, boolean, boolean, boolean, boolean, boolean]) => boolean[]' is not assignable to parameter of type '(value: [boolean, boolean, boolean, boolean, boolean, boolean]) => [boolean]'.
  Type 'boolean[]' is not assignable to type '[boolean]'.
    Target requires 1 element(s) but source may have fewer.

I can tell TypeScript specifically that the return type is indeed [boolean] with the following syntaxes:

(value): [boolean] => [value.some((element) => (element))] // OK
(value) => [value.some((element) => (element))] as [boolean] // OK

🔎 Search Terms

tuple

🕗 Version & Regression Information

I have TypeScript version 4.9.5 and work on VS Code 1.77.3.

⏯ Playground Link

💻 Code

(value) => [value.some((element) => (element))]
// (parameter) value: [boolean, boolean, boolean, boolean, boolean, boolean]
// => [boolean]

// Error
Argument of type '(value: [boolean, boolean, boolean, boolean, boolean, boolean]) => boolean[]' is not assignable to parameter of type '(value: [boolean, boolean, boolean, boolean, boolean, boolean]) => [boolean]'.
  Type 'boolean[]' is not assignable to type '[boolean]'.
    Target requires 1 element(s) but source may have fewer.

🙁 Actual behavior

I get the following error:
Argument of type '(value: [boolean, boolean, boolean, boolean, boolean, boolean]) => boolean[]' is not assignable to parameter of type '(value: [boolean, boolean, boolean, boolean, boolean, boolean]) => [boolean]'.
Type 'boolean[]' is not assignable to type '[boolean]'.
Target requires 1 element(s) but source may have fewer.

🙂 Expected behavior

I would expect to not get any error, since I used the "some" method that returns a boolean, and I put it in an array with [], which insures that I have one element. For me, the types seem coherent.
How could TypeScript handle this correctly ?

Activity

  1. MartinJohns commented on Apr 19, 2023

    @MartinJohns
    Contributor

    If there's no contextual type information available to indicate that the result should be a tuple TypeScript errs on the side of returning an array. This is by design.

  2. jonathanfady commented on Apr 19, 2023

    @jonathanfady
    Author

    Ok, but in this case the returned value can't be anything than a tuple with one element, e.g. [boolean], right ?
    Couldn't TypeScript have a way of figuring it out ? What is the context for this design choice ?

  3. MartinJohns commented on Apr 19, 2023

    @MartinJohns
    Contributor

    In most cases the values are used as arrays, and inferring a tuple instead would be cumbersome (and it would have been a massive breaking changes, as tuples were added way after arrays).

    If contextually it can be inferred that it's to be used as a tuple, it will be a tuple. Otherwise you can add a type annotation.

  4. RyanCavanaugh commented on Apr 19, 2023

    @RyanCavanaugh
    Member

    In general inferring the very-narrowest type for a function leads to suboptimal behavior in a lot of cases. Inference tries to pick the type that accurately reflects what's going on without being overspecific, but can't always know what you meant to do.

  5. jonathanfady commented on Apr 20, 2023

    @jonathanfady
    Author

    Ok, thanks for your responses.
    Too bad for the tuple lovers out there, but I guess it makes sense that the tuple is the new kid on the block and still needs to prove his worth to be deemed inferable against the big array boss !
    Maybe this "tuple auto inference" could be part of the ts configuration options ?
    Have a nice day !

  6. fatcerberus commented on Apr 20, 2023

    @fatcerberus

    Jonathan Fady (@jonathanfady) You can write [..] as const to get TS to infer a tuple type in cases where it can’t do so automatically. This is safer than using an explicit type assertion like as [boolean].

  7. jonathanfady commented on Apr 21, 2023

    @jonathanfady
    Author

    Jonathan Fady (@jonathanfady) You can write [..] as const to get TS to infer a tuple type in cases where it can’t do so automatically. This is safer than using an explicit type assertion like as [boolean].

    Bruce Pascoe (@fatcerberus) Not sure this works as you expected. If I use my previous example, I get the following error in the editor with as const :

    (value) => [value.some((element) => (element))] as const
    
    The type 'readonly [boolean]' is 'readonly' and cannot be assigned to the mutable type '[boolean]'.ts(4104)
    

    Do you have any thoughts on this ?

  8. typescript-bot commented on Apr 23, 2023

    @typescript-bot
    Contributor

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

  9. locked as resolved and limited conversation to collaborators on Oct 22, 2025
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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions