Returns true if an array is empty.
Allows typescript to understand the array is empty when used in an if statement:
const arr = []if (isEmpty(arr)) { // arr is []} else { // arr is any[]}// more examples below Copy
const arr = []if (isEmpty(arr)) { // arr is []} else { // arr is any[]}// more examples below
Also useful for objects, but there is no way to get typescript to understand the object is empty.
const obj = {a:"a"}if (isEmpty(keys(obj))) { // obj is still {a: string}} else { // obj is {a: string}} Copy
const obj = {a:"a"}if (isEmpty(keys(obj))) { // obj is still {a: string}} else { // obj is {a: string}}
More examples of how this behaves with different array types:
const arr: any[] = [] // implicit any[]if (isEmpty(arr)) arr // []else arr // any[]const arr = ["A"]if (isEmpty(arr)) arr // []else arr // string[]const arr: [] = []// ^ required for non-const arraysif (isEmpty(arr)) arr // []else arr // neverconst arr: ["A"] = ["A"]// ^ required for non-const arraysif (isEmpty(arr)) arr // neverelse arr // ["A"]const arr = [] as constif (isEmpty(arr)) arr // readonly []else arr // neverconst arr = ["A"] as constif (isEmpty(arr)) arr // neverelse arr // readonly ["A"] Copy
const arr: any[] = [] // implicit any[]if (isEmpty(arr)) arr // []else arr // any[]const arr = ["A"]if (isEmpty(arr)) arr // []else arr // string[]const arr: [] = []// ^ required for non-const arraysif (isEmpty(arr)) arr // []else arr // neverconst arr: ["A"] = ["A"]// ^ required for non-const arraysif (isEmpty(arr)) arr // neverelse arr // ["A"]const arr = [] as constif (isEmpty(arr)) arr // readonly []else arr // neverconst arr = ["A"] as constif (isEmpty(arr)) arr // neverelse arr // readonly ["A"]
Returns true if an array is empty.
Allows typescript to understand the array is empty when used in an if statement:
Also useful for objects, but there is no way to get typescript to understand the object is empty.
More examples of how this behaves with different array types: