Last active
February 23, 2022 15:40
-
-
Save Puppo/bc5287d32dbfeeba9f14862654cece9d to your computer and use it in GitHub Desktop.
NotEmptyArray definition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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