Skip to content

Indexing tuple intersection type beyond length produces unexpected typeΒ #42557

Open

Description

@tomblind

Bug Report

πŸ”Ž Search Terms

tuple intersection index type

⏯ Playground Link

Playground Link

πŸ’» Code

type T = [number, boolean] & { x: string };
declare const t: T;
const a = t[0]; // number
const b = t[1]; // boolean
const c = t[2]; // number | boolean !?

πŸ™ Actual behavior

c is typed as number | boolean

πŸ™‚ Expected behavior

c should be typed as undefined

I'm not sure if this is a bug, or intended behavior. But it was surprising. If the type is just a tuple, indexing beyond its length produced undefined. But if it is part of an intersection, the element type is changed to number | boolean, as if it is no longer a tuple, but an array. However, indexing 0 or 1 still produces the correct type for those element positions.

Activity

  1. 0xPoe commented on Feb 2, 2021

    @0xPoe

    Ryan Cavanaugh (@RyanCavanaugh) I would like to help fix the issue. Can you help with more information on fixing it?

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

    BugA bug in TypeScriptDomain: IntersectionIncorrect behavior under `T & U` constructsHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions