Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ArrayAt type for stricter Array.prototype.at() with tuples #369

Open
jonahsnider opened this issue Mar 1, 2022 · 10 comments
Open

Add ArrayAt type for stricter Array.prototype.at() with tuples #369

jonahsnider opened this issue Mar 1, 2022 · 10 comments
Labels
help wanted Extra attention is needed type addition

Comments

@jonahsnider
Copy link
Contributor

jonahsnider commented Mar 1, 2022

See sindresorhus/ts-extras#39 and microsoft/TypeScript#47660.

const tuple = ['abc', 123, true] as const;

type First = ArrayAt<typeof tuple, 0>; // 'abc'
type Last = ArrayAt<typeof tuple, -1>; // true
type SecondToLast = ArrayAt<typeof tuple, -2>; // 123
type ThirdToLast = ArrayAt<typeof tuple, -3>; // 'abc'
type OutOfBounds = ArrayAt<typeof tuple, 999>; // should not compile since 999 does not extend 0 | 1 | 2 | -1 | -2 | -3
const array = ['abc', 123, true];

type First = ArrayAt<typeof tuple, 0>; // string | number | true
type Last = ArrayAt<typeof tuple, -1>; // string | number | true
type OutOfBounds = ArrayAt<typeof tuple, 999>; // string | number | true

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus
Copy link
Owner

PR welcome

@sindresorhus sindresorhus added type addition help wanted Extra attention is needed labels Mar 1, 2022
@sindresorhus
Copy link
Owner

Per discussion in sindresorhus/ts-extras#39, should this be TupleAt?

@jonahsnider
Copy link
Contributor Author

If we call this type TupleAt how would behavior with non-tuples work? Disallow non-tuple inputs? Rename but still support plain arrays?

@sindresorhus
Copy link
Owner

Why would you use it with non-tuples? It has no benefit.

@jonahsnider
Copy link
Contributor Author

I'm thinking of cases where users might have a type param for an array or a tuple which is passed to this type. Might be good to gracefully handle those. Not sure if this would be an actual pain point though.

@sindresorhus
Copy link
Owner

Then I suggest we do two types:

  • TupleAt
  • ArrayAt

@jonahsnider
Copy link
Contributor Author

What would the behavior of ArrayAt be? Would it be any different than typeof array[number]?

@tychenjiajun
Copy link

What would the behavior of ArrayAt be? Would it be any different than typeof array[number]?

ArrayAt<Array, -1> should be the same as LastArrayElement<Array>

@skarab42
Copy link
Collaborator

For arrays I would expect string and boolean for First and Last respectively.

const array = ['abc', 123, true];

type First = ArrayAt<typeof array, 0>; // string
type Last = ArrayAt<typeof array, -1>; // boolean

@papb
Copy link
Contributor

papb commented Sep 2, 2022

Then I suggest we do two types:

  • TupleAt
  • ArrayAt

@sindresorhus But what if we have an array type T that sometimes is a tuple and sometimes not? If TupleAt and ArrayAt are segregated, then we won't be able to use any of them with T. I propose the following:

type T = number[] | [true, false];
type X = ArrayAt<T, -1>; // Should be `number | false`, IMO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed type addition
Projects
None yet
Development

No branches or pull requests

5 participants