-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Comments
I think the problem here is that, only looking at 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:
This isn't inferrable either. The rest element |
@fatcerberus Thanks. This makes sense. The first rest element But, since
|
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?
*/ |
|
You're assuming the compiler knows what |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
tuples
,rest elements
,invalid infer
π Version & Regression Information
Tested version:
Version 4.5.5
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
All elements (index from
0 to rt.length - 1
) have the same typestring | boolean | number
π Expected behavior
Typescript should infer:
rt[rt.length - 1]
is aboolean
rt[rt.length - 2]
is anumber
rt[0] to rt[rt.length - 3]
inclusive arestring
sThe text was updated successfully, but these errors were encountered: