TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.
An Elegant and Safe Solution for the Strict Typing of Array.includes
Array.includes is a commonly used function in JavaScript. However, in TypeScript, its typing is quite strict: The element being searched for must have the same type as that of the array element. This causes type errors if the array is of a literal type. For example, with the following code snippet: const okNumbers = [1, 3, 5, 7] as const; console.log(`2 is OK? ${okNumbers.includes(2)}`); The TypeScript compiler complains: Argument of type ‘2’ is not assignable to parameter of type ‘1 | 3 | 5 | 7’. ...