Closed
Description
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:
rt[rt.length - 1]
is aboolean
rt[rt.length - 2]
is anumber
- from
rt[0] to rt[rt.length - 3]
inclusive arestring
s