#Is this function optimized for Empty Array verification?

9 messages · Page 1 of 1 (latest)

grim kiln
#

I have a small snippet that I'm trying to use for the verification of an empty array. Basically, it should check if there's at least one element in the array. If there is one, returns false, otherwise true.
Is this the most optimized way to create this function?

 type EmptyArr<T extends any[]> = T['length'] extends 0 ? true : false

type E1 = [1,2,3] //false
type E2 = []      //true

type T1 = EmptyArr<E1>
maiden fox
#

Not sure what you mean by "fully optimized" here.

#

Is this some sort of type-level metaprogramming project or is this intended to be used with JS code?

grim kiln
junior sandal
#

so you definition of "optimized" is basically "shorter"? 🤔

#

but to answer your question: no, I don't think there is a shorter way to do that

merry pewter
#

T extends readonly [] would be marginally faster since there's no indexed access type right

junior sandal