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

Proposal: IndexesOf and ItemsOf #670

Closed
lucasteles opened this issue Aug 24, 2023 · 2 comments · Fixed by #720
Closed

Proposal: IndexesOf and ItemsOf #670

lucasteles opened this issue Aug 24, 2023 · 2 comments · Fixed by #720
Labels
help wanted Extra attention is needed type addition

Comments

@lucasteles
Copy link
Contributor

This is a way to have typed indexes of a tuple, useful when you have types deriving of a const value

definition:

type ItemOf<T extends readonly any[]> = T[number]
type IndexesOf<T extends readonly any[]> = Exclude<Partial<T>['length'], T['length']>

use case:

const WEEK_DAYS = [
  'Monday', 'Tuesday', 'Wednesday',
  'Thursday', 'Friday', 'Saturday',
  'Sunday' ] as const

type WeekDayName = ItemsOf<typeof WEEK_DAYS>
type WeekDay = IndexesOf<typeof WEEK_DAYS>

const getDayName = (day: WeekDay): WeekDayName => WEEK_DAYS[day]
@sindresorhus
Copy link
Owner

Related: #658

I'm not sure which names are best ItemsOf or ArrayValue.

@sindresorhus sindresorhus added help wanted Extra attention is needed type addition labels Aug 24, 2023
@lucasteles
Copy link
Contributor Author

@sindresorhus ArrayValue and ArrayIndex looks good

This was referenced Oct 20, 2023
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

Successfully merging a pull request may close this issue.

2 participants