Skip to content

Proposal: ArrayItem #118

@scvnathan

Description

@scvnathan

We've found this type handy in our codebase and thought this would be useful in type-fest too.

Proposal

ArrayItem extracts the type of the item(s) in the array:

type ArrayItem<TArray extends any[]> = TArray extends (infer E)[] ? E : TArray;

Possibly could be improved by replacing the fallback conditional with unknown instead of TArray.

playground example: https://www.typescriptlang.org/play/#code/C4TwDgpgBAggTnAhiAksCBbAPAFXkkKCAD3QDsATAZykTJAG0BdAPigF4o8FkjSJKNABQBLMgDMIcKAFEAlMygB+WVABcXfMgDcAKF2hIUNJhqcA3lHEB7axqrA4YgObaoAI0RwNZAK4Z3KSgAX2Y9Q2gAWRFiCAoTDDMoIUsbOygHJzJXDy8ff0DpYKgAMihLTwAvDQAhEWcUMnRnIOCFJj0DcGgRdAwOWB5UPqwEqhZw7qgMGLiEga1hzCxo2Pi+8cmjMQA3RAAbEQoFoYSsPwCpCaA

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions