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: ArrayValue type #49060

Closed
5 tasks done
avaly opened this issue May 11, 2022 · 1 comment
Closed
5 tasks done

Proposal: ArrayValue type #49060

avaly opened this issue May 11, 2022 · 1 comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@avaly
Copy link

avaly commented May 11, 2022

Suggestion

πŸ” Search Terms

array type
array value
array value type

βœ… Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

An utility type to obtain an array's items type.

type Value = ArrayValue<T[]>; // T

πŸ“ƒ Motivating Example

TS 4.5 introduced the Awaited utility type for promises. This proposals adds an utility type with similar syntax for arrays.

πŸ’» Use Cases

When building components for a list UI and need to use some API generated types (e.g. GraphQL generated types):

type Item = ArrayValue<GeneratedResponse['nodes']>;

Current workaround: using Foo[number], but it's less readable.

@MartinJohns
Copy link
Contributor

See #39522 (comment):

We've opted to not include utility type aliases in the lib unless they're required for declaration emit purposes.


Personally I don't see much value, when it's trivially done using T[number] already.

type ArrayType<T extends any[]> = T[number]
type Foo = ArrayType<number[]>
//   ^?

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels May 11, 2022
@avaly avaly closed this as completed May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants