Created
July 20, 2022 10:25
-
-
Save thebuilder/b22edc9882b5208d27a6719f08309eca to your computer and use it in GitHub Desktop.
TypeScript: Pick type of item in an Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get an element from an array | |
* @example | |
* interface Item { | |
* id: number | |
* } | |
* | |
* interface BunchOfItems extends Array<Item> {} | |
* | |
* interface SingleItemAgain extends ArrayElement<BunchOfItems> {} | |
* | |
* // You can use it again as a single item | |
* const test: SingleItemAgain = { | |
* id: 123 | |
* } | |
*/ | |
type ArrayElement<Type> = Type extends Array<infer Item> ? Item : Type; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment