Last active
May 12, 2022 18:22
-
-
Save mannyanebi/a4ee358610159789ea12d252323a9c6b to your computer and use it in GitHub Desktop.
How to extract a type from 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
// This is possible with Typescript 2.8+. You can declare an Unpacked<T> type as follows: | |
type Unpacked<T> = T extends (infer U)[] ? U : T; | |
type InnerCacheType = Unpacked<CacheType>; // Event | User | |
// This is a condensed definition of the Unpacked type given in the documentation. | |
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html | |
type Unpacked<T> = T extends Array<infer U> ? U : T extends ReadonlyArray<infer U> ? U : T; | |
// this works with ReadonlyArray as well – |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment