Utilities for determining whether a value is in a given Array/Set.
JavaScript Array.includes and Set.has determine whether a value is in a given
Array/Set. They are typed such that the parameter must be assignable to the
element type of the Array/Set:
constfruits = ["apple", "orange", "grape"]; // or new Set(["apple", "orange", "grape"]) constfireball: string = "fire ball"; constcontainingFireball = fruits.includes(fireball); // or fruits.has(fireball); OK constcontainingNumber1 = fruits.includes(1); // or fruits.has(1); // error: Argument of type 'number' is not assignable to parameter of type 'string'.
This makes sense, because when fruits is dynamically constructed by the logic of the program,
an unmatched element type is not intended and likely an error. However, the third line in the
above code snippet would fail to compile if fruits contains literal types:
constfruits = ["apple", "orange", "grape"] asconst; // or new Set(["apple", "orange", "grape"] as const); constfireball: string = "fire ball"; constcontainingFireball = fruits.includes(fireball); // or fruits.has(fireball); // error: Argument of type 'string' is not assignable to parameter of // type '"apple" | "orange" | "grape"'.
This doesn't make sense because the semantics has changed: fireball doesn't have to have one of
the literal string types in fruits.
To address this issue, this module provides a isOneOf function that is typed to
accommodate the second scenario above, while retains the same logic as Array.includes
and Set.has:
constfruits = ["apple", "orange", "grape"] asconst; // or new Set(["apple", "orange", "grape"] as const); constfireball: string = "fire ball"; constcontainsFireball = isOneOf(fireball, fruits); // OK
Utilities for determining whether a value is in a given Array/Set.
JavaScript Array.includes and Set.has determine whether a value is in a given Array/Set. They are typed such that the parameter must be assignable to the element type of the Array/Set:
This makes sense, because when
fruits
is dynamically constructed by the logic of the program, an unmatched element type is not intended and likely an error. However, the third line in the above code snippet would fail to compile iffruits
contains literal types:This doesn't make sense because the semantics has changed:
fireball
doesn't have to have one of the literal string types infruits
.To address this issue, this module provides a isOneOf function that is typed to accommodate the second scenario above, while retains the same logic as Array.includes and Set.has:
For the first scenario, you should continue using Array.includes and Set.has.
This function can also be used as a type guard. Continuing the previous example: