question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature: ability to extract union of valid index numbers for tuple

See original GitHub issue

Search Terms

indexof array indexof tuple restrict argument to tuple index

Suggestion

I’d like the ability to restrict a type to a valid index number of a tuple. For example, given the tuple [string], I’d like to extract the type 0. Given the tuple [string, number, object], I’d like to extract the type 0 | 1 | 2.

Use Cases

see related S.O. question

Currently, if a generic class receives a type argument which is a tuple, it isn’t possible to create a method on the class which restricts its argument to a valid index of that tuple.

Examples

class FormArray<T extends [any, ...any[]]> {
  constructor(public value: T) {}

  // how to restrict `I` to only valid index numbers of `T` ?
  get<I extends keyof T>(index: I): T[I] {
    return this.value[index];
  }
}

Implementation ideas

I imagine there are several routes to achieving my goal.

Idea 1 (perhaps heavy handed):

Add a new keyword, such as indexof which can only be used on arrays and tuples. If indexof is used on an array, it will always return number. If indexof is used on a tuple, it returns a union of 0 .... tuple length - 1 (e.g. if the tuple was of length 3 indexof would return 0 | 1 | 2).

Idea 2:

Ability to cast a string type to the equivalent number type. For example, the ability to cast type "0" to 0. This would allow using the keyof keyword to get all properties of a tuple, cast property types to numbers (if possible), and then filter out any property types which aren’t numbers (i.e. filter out "length", "splice", etc. and leave 0 | 1 | 2).

For example, as pointed out in this comment, it is currently possible to get the indexes of a tuple in string form (i.e. "0" | "1" | "2").

type ArrayKeys = keyof any[];
type Indices<T> = Exclude<keyof T, ArrayKeys>;

Indices<[string, number]>; // "0" | "1"

Idea 3:

As pointed out in a comment, you can get the index numbers of a tuple using the following type:

type Indices<T extends {length: number}> = Exclude<Partial<T>["length"], T['length']>;

Unfortunately, the result of this type is not considered a keyof the input tuple (which results in a type error if you try and use the type as a key for the tuple). If there were some way of using a type assertion to tell the compiler that this is, in fact, a keyof T, that might also work.

note: this type differs from the type presented in idea 2 (above) because, unlike this type, the type in idea 2 is a keyof T

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
jack-williamscommented, Aug 15, 2019

@AlCalzone Do you need Drop1? Can you not just exclude the length of the original tuple?

2reactions
AlCalzonecommented, Aug 15, 2019

Partial<T>[“length”]

Damn! Never thought of this… 👀 But that includes one union item too much.

Partial<Drop1<T>>["length"] works with

type Drop1<T extends any[]> = ((...args: T) => void) extends ((a1: any, ...rest: infer R) => void) ? R : never;
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to restrict method argument to index number of tuple type?
Here, we can successfully extract the property index numbers of the tuple if the tuple is of length 13 or less. Otherwise, we...
Read more >
Arrays & Tuples - Type-Level TypeScript
We can do the same with tuples by using a union of number literal types! ... A lesser-known feature of tuples is their...
Read more >
Recitation 2: Tuples, Records, Datatypes
You can extract the first two components of a tuple by using the fst and snd operators, which retreive the first and second...
Read more >
TypeScript Tuples - GeeksforGeeks
Accessing tuple Elements With the help of index basis we can read or access the fields of a tuples, which is the same...
Read more >
Python Basics: Tuples - Towards Data Science
Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking or indexing (or even by attribute ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found