Skip to content

Mapping of a tuple with array functions causes an error. #17591

Closed

Description

@Thomas-P

TypeScript Version: 2.4.2

Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
//
// create a sample array of a tuple, of type (string, number)
//
const testArray: Array<[string, number]> = [['A',3], ['B', 7]];
//
// convert them to an array of a tuple, of type (string, string, number)
//
const mapToArray: Array<[string, string, number]> = testArray.map(([str, num]) => [str, str, num]);

Expected behavior:
should work fine
Actual behavior:

error TS2322: Type '(string | number)[][]' is not assignable to type '[string, string, number][]'.
Type '(string | number)[]' is not assignable to type '[string, string, number]'.
Property '0' is missing in type '(string | number)[]'.

Workaround
Add the result type to the map function, like this:

//
// create a sample array of a tuple, of type (string, number)
//
const testArray: Array<[string, number]> = [['A',3], ['B', 7]];
//
// convert them to an array of a tuple, of type (string, string, number)
//
const mapToArray: Array<[string, string, number]> = testArray.map(([str, num]):[string, string, number] => [str, str, num]);

But it themes to be a problem of converting a result type by checking the return type and map them to the outer function type.

Activity

  1. Thomas-P commented on Aug 3, 2017

    @Thomas-P
    Author

    Ok, I updated to version 2.4.2 and could reproduce this in this version.

  2. kitsonk commented on Aug 3, 2017

    @kitsonk
    Contributor

    Tuples are never inferred when the type is ambiguous, which it is in this case.

    See: #11924 (issue comment)

    There is a long discussion here about inferring literals including tuples: #10195

  3. Thomas-P commented on Aug 3, 2017

    @Thomas-P
    Author

    ok, then I will close the issue.

  4. locked and limited conversation to collaborators on Jun 14, 2018
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