Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 6
    This implementation infers the element type from the array type, while the accepted answer infers the type of the first element in the array. I just thought it was more elegant, but since the array is guaranteed (by its type definition) to only contain elements of the same type, there's no technical advantage to using this solution. Commented Nov 20, 2018 at 11:10
  • 7
    Typescript supports indexed based array element types such as: [number,string]. So this answer has some advantages as ArrayElement<[number,string]> -> number | string Commented Apr 2, 2019 at 2:07
  • 1
    this should no longer be the accepted answer, as described here Commented Jan 24, 2021 at 21:36
  • 2
    I agree with @feihcsim. My answer may have issues if the --noUncheckedIndexedAccess flag is enabled, whereas @jerico's answer wouldn't. That'd be enough of a reason for me to choose that answer over my one, which is a little sad since ArrayType[number] is much easier to read, especially for beginners. Commented Jan 28, 2021 at 17:01
  • 3
    @feihcsim I'm pretty sure this answer did not need to change and the original answer (ArrayType[number]) still works fine, even with --noUncheckedIndexedAccess enabled. That flag does not affect these type-level operations. Commented Dec 11, 2021 at 15:48