Skip to content

CastArray type #1392

@Laurens256

Description

@Laurens256

Type description + examples

CastArray is the return type of this util function:

const castArray = <T>(value: T | T[]): T[] => {
  return Array.isArray(value) ? value : [value];
};

import type { CastArray } from 'type-fest';

type Foo = string;
type FooArr = string[];

type CastFoo = CastArray<Foo>; // -> string[];
type CastFooArr = CastArray<FooArr>; // -> string[]

Type source

Could be something like this. Not completely sure if this covers all edge cases someone might want to use this for. For example if you want the return type [T] instead of T[]

type CastArray<T> = T extends any[] ? T : T[];

Search existing types and issues first

  • I tried my best to look for it

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions