Skip to content

The intersection of a tuple with an unrelated array is handled inconsistentlyΒ #53355

Open
@geoffreytools

Description

@geoffreytools

Bug Report

πŸ”Ž Search Terms

tuple, intersection, array, unrelated, narrowing, index signature

πŸ•— Version & Regression Information

This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

/* unrelated array */

type ShouldBeNever = string[] & [1, 2, 3]

type OK = (ShouldBeNever)[number] // never

type NotOK = (ShouldBeNever)[1] // 2

type WeirdOverload = (ShouldBeNever)['pop'] // {
//    (): string | undefined
//    (): 3 | 1 | 2 | undefined
//}

const isString = ([1, 2, 3] as ShouldBeNever).pop() // string | undefined


/* related array but not a supertype */

type ShouldBeNarrowed = [number | string, number | boolean] & (string | boolean | symbol)[]

type FF = ShouldBeNarrowed[number] // string | boolean
type HH = ShouldBeNarrowed[0] // string | number

πŸ™ Actual behavior

The index signature is appropriately updated, but not the individual elements nor the methods operating on the resulting tuple. Additionally, methods get overloaded, so when calling pop for example, the first overload is selected and the return type is not the intersection of the original components return types as it should be.

πŸ™‚ Expected behavior

I would expect ShouldBeNever to be [never, never, never] and ShouldBeNarrowed to be [string, boolean]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Experimentation NeededSomeone needs to try this out to see what happensSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions