9

Is there a Typescript way to require that an array needs to have at least one value? e.g:

type value = "1" | "2";

export interface ISomething {
  values: value[] // < should be required and have at least one.
}

4 Answers 4

12

TL;DR: type NonEmptyArray<T> = [T, ...Array<T>]


Following your example:

type value = "1" | "2";

export interface ISomething {
  values: [value, ...value[]] // ← this is the non-empty array type
}

const empty: ISomething = { values: [] }
// Type '[]' is not assignable to type '[value, ...value[]]'.
// Source has 0 element(s) but target requires 1.

const oneValue: ISomething = { values: ["1"] }
// all good :)

Optionally, you could extract it to be used as a generic utility type, as follows:

export type NonEmptyArray<T> = [T, ...Array<T>];

export interface ISomething {
  values: NonEmptyArray<value>
}
Sign up to request clarification or add additional context in comments.

Comments

7

Another way without interfaces:

export type NonEmptyArray<Type> = [Type, ...Array<Type>];

Usage:

const arrayOfStrings1: NonEmptyArray<string> = ['x']; // valid
const arrayOfStrings2: NonEmptyArray<string> = ['x', 'y']; // valid
const arrayOfStrings3: NonEmptyArray<string> = ['x', 'y', 'z']; // valid
const arrayOfStrings4: NonEmptyArray<string> = [] // invalid
// Type '[]' is not assignable to type '[string, ...string[]]'.
// Source has 0 element(s) but target requires 1.ts(2322)

Comments

1

Try this

type value = "1" | "2";

export interface ISomething {
  values: {
    0: value,
    [key: number]: value,
  }
}``

Comments

0

You could also implement an interface that extends Array:

type value = "1" | "2";

interface IValueArray extends Array<value> {
  0: value; // ensure that at least one 'value' is present
}

export interface ISomething {
  values: IValueArray
}

const something1: ISomething = {
  values: ['1']
}

const something2: ISomething = {
  values: [] // type error
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.