Skip to content

Tuples that have rest elements types not properly inffering #47863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
renjithspace opened this issue Feb 12, 2022 · 6 comments
Closed

Tuples that have rest elements types not properly inffering #47863

renjithspace opened this issue Feb 12, 2022 · 6 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@renjithspace
Copy link

renjithspace commented Feb 12, 2022

Bug Report

πŸ”Ž Search Terms

tuples, rest elements, invalid infer

πŸ•— Version & Regression Information

Tested version: Version 4.5.5

  • I didn't test this on prior versions because I think it's not a version-specific behavior

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type RestTuple = [...string[], number, boolean]

const rt: RestTuple = ["hello", "world", "!", 42, true]

const [rt0, rt1, rt2, rt3, rt4] = rt

/**
 * actual types: rt0, rt1, rt2, rt3 and rt4 has a type 'string | number | boolean'
 * 
 * expected types:
 *    rt0 is string
 *    rt1 is string
 *    rt2 is string
 *    rt3 is number
 *    rt4 is boolean
 * 
 * Because, from RestTuple, Typescript obviously infer:
 *    rt[0] will be a string
 *    rt[rt.length - 1] is a boolean
 *    rt[rt.length - 2] is a number
 *    so, the remaining indexes rt[0 + 1] and rt[0 + 2] will be string
 */

πŸ™ Actual behavior

All elements (index from 0 to rt.length - 1) have the same type string | boolean | number

πŸ™‚ Expected behavior

Typescript should infer:

  1. rt[rt.length - 1] is a boolean
  2. rt[rt.length - 2] is a number
  3. from rt[0] to rt[rt.length - 3] inclusive are strings
@fatcerberus
Copy link

fatcerberus commented Feb 12, 2022

I think the problem here is that, only looking at the type RestTuple, the compiler has no way to know how many elements the ...string[] part constitutes. You know there are 3 string elements, because you just assigned the tuple on the previous line, but this information isn't encoded in the type.

The issue becomes clearer if you modify the code like this:

type RestTuple = [ ...string[], number, boolean ];

function foo(rt: RestTuple) {
    // what types are these? who knows?
    const [ rt0, rt1, rt2, rt3, rt4 ] = rt;
}

foo([ "hello", "world", "!", 42, true ]);
foo([ "godzilla", "?", 777, true ]);
foo([ "eaty pig", 812, false ]);
foo([ 1208, false ]);

Also, just for the record:

rt[0] will be a string

This isn't inferrable either. The rest element ...string[] can be empty.

@renjithspace
Copy link
Author

@fatcerberus Thanks. This makes sense.

The first rest element ...string[] can be empty. So thert[0] can't always infer as string, it can be string | number.

But, since RestTuple's all elements are required:

  1. how rt[0] and rt[rt.length - 2] can be a boolean?
  2. how rt[rt.length - 1] can be string | number?

@renjithspace
Copy link
Author

renjithspace commented Feb 12, 2022

I think this is also an example that might relate to not inferring the length of arrays as literals when it is possible and obvious. I know there's might a reason but honestly, I don't know why.

let x: "hello" = "hello"
const y = "world"
const xl = y.length // actual xl type is number, expected 5
const yl = y.length // actual yl type is number, expected 5

/**
 * πŸ“Œ x has a string literal type "hello"
 * y is a constant
 * 
 * πŸ“Œ  since x and y's value never gonna change,
 * x and y's length never gonna change.
 * 
 * ❓ So why typescript can't infer length as a number literal type 5
 * instead of primitive type 'number' in this 'obvious' situation?
 */

@MartinJohns
Copy link
Contributor

I think this is also an example that might relate to not inferring the length of arrays as literals when it is possible and obvious.

#34692

@fatcerberus
Copy link

fatcerberus commented Feb 12, 2022

how rt[rt.length - 1] can be string | number?

You're assuming the compiler knows what rt.length is. It doesn't. Refer back to my example of passing different-sized tuples to a function.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 14, 2022
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants