I got a couple of ways to define a NonEmptyArray from this question. They are mostly working but I get a weird thing when I use it with a code created array:
type NonEmptyArray<T> = T[] & { 0: T };
function myFunction(param: NonEmptyArray<number>){
}
const myArray = [1,2];
myFunction([1,2]); // OK
myFunction(myArray); // ERROR
The error says:
Argument of type 'number[]' is not assignable to parameter of type 'NonEmptyArray'. Property '0' is missing in type 'number[]' but required in type '{ 0: number; }'.(2345)
If I define the type hard then the error is gone:
type NonEmptyArray<T> = T[] & { 0: T };
function myFunction(param: NonEmptyArray<number>){
}
const myArray: number[] & {0: number} = [1, 2];
myFunction([1,2]); // OK
myFunction(myArray); // Also OK
I am not sure if this is a bug in the type definition or in typescript or I am not seeing something. Does anyone knows what is happening here?