Skip to content

Instantly share code, notes, and snippets.

@btoo
Last active August 14, 2019 19:32
Show Gist options
  • Select an option

  • Save btoo/ee68f1326cadfc715739f80863595d56 to your computer and use it in GitHub Desktop.

Select an option

Save btoo/ee68f1326cadfc715739f80863595d56 to your computer and use it in GitHub Desktop.
infer the type of an element in an array from the array's type
type ArrayElement<ArrayType> =
ArrayType extends (infer ElementType)[] ? ElementType :
ArrayType extends ReadonlyArray<infer ElementType> ? ElementType :
never;
type Flatten<T> = T extends Array<infer U> ? U : T;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment