Q:

Defining a non-empty list through a type

I'm wondering if it's possible to define a non-empty list in the type system with typescript. I know that I could define a list of n elements, similar to a Tuple:
type TwoElementList = [number, number];

But this would only work for lists of a predefined length.
I'm looking at the popular CIS 194 course on Haskell, which gives an example of defining a non-empty list using the type system:
data NonEmptyList a = NEL a [a]

I think this isn't possible in typescript; but I am wondering if anyone can confirm that.

In the part of CIS 914 which defines NonEmptyList, we see the conversions between this 'interface' and that of a standard list:
nelToList :: NonEmptyList a -> [a]
nelToList (NEL x xs) = x:xs

listToNel :: [a] -> Maybe (NonEmptyList a)
listToNel []     = Nothing
listToNel (x:xs) = Just $ NEL x xs

So as @JeffreyWesterkamp points out, an equivalent in typescript would indeed have a different interface than a standard list, and indeed we could create functions to translate between the interface [number, [number]] and [number].
To illustrate what I'm getting at, though, consider the following typescript:
type NumberPair = [number, number];

const x: NumberPair = [1, 1];
const y = x[2];

Here Typescript will show a compilation error because we cannot access [2] on NumberPair. Similarly for lists of non-fixed length, the type of an element at an index is the union T | undefined; understandable because we don't know if the index i is present on a list. But in a non-empty list, we can always guarantee that the index 0 is present. What I'm wondering is whether it's possible to have typescript determine from a type signature that it will always be able to get at least 1 element from a type.

A:

Yes it is indeed possible!
type NonEmptyArray<T> = [T, ...T[]];

