Skip to content

Instantly share code, notes, and snippets.

@Puppo
Last active February 23, 2022 15:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Puppo/bc5287d32dbfeeba9f14862654cece9d to your computer and use it in GitHub Desktop.
Save Puppo/bc5287d32dbfeeba9f14862654cece9d to your computer and use it in GitHub Desktop.
NotEmptyArray definition
export interface NonEmptyArray<A> extends ReadonlyArray<A> {
// tslint:disable-next-line: readonly-keyword
0: A;
}
type ReadOnlyNotEmptyArray<T> = Readonly<NotEmptyArray<T>>;
function isNotEmptyArray<T>(as: T[]): as is NotEmptyArray<T> {
return as.length > 0;
}
function freeze(as: NotEmptyArray<T>): ReadOnlyNotEmptyArray<T> {
return Object.freeze(as);
}
const head = <T>(as: NotEmptyArray<T>): T => as[0];
const tail = <T>([, ...rest]: NotEmptyArray<T>): T[] => rest;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment