Skip to content

Tuples that have rest elements types not properly inffering Β #47863

Closed
@renjithspace

Description

@renjithspace

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

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