Skip to content

Negative tuple indexer should not be valid #39293

@cntkillme

Description

@cntkillme

TypeScript Version: 4.0.0-dev.20200625

Search Terms:
negative tuple indexer

Code

type t1 = [string, number][0]; // string
type t2 = [string, number][1]; // number
type t3 = [string, number][-1]; // (string | number)  ???

type u<t extends any[], u extends number> = t[u];

type u1 = u<[string, number], 0>; // string
type u2 = u<[string, number], 1>; // number
type u3 = u<[string, number], 2>; // undefined
type u4 = u<[string, number], 1.5>; // undefined
type u5 = u<[string, number], -0.5>; // (string | number)  ???

Expected behavior:
that type t3 = [string, number][-1]; emits a compile-time error "Tuple type '[string, number]' of length '2' has no element at index '-1'." and u5 is undefined.

Actual behavior:
t3 and u5 is a union of the types contained in the tuple.

Playground Link: playground link

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions