Re:A 'CSS reset' for TypeScript, improving types for common ...
by jeroenhd on Saturday March 04, 2023 @04:02AM
(#35019400)
But TypeScript does have typed strings. If the array contains arbitrary strings, then the includes() call would just succeed without error.
Re:A 'CSS reset' for TypeScript, improving types for common ...
by Dylan16807 on Saturday March 04, 2023 @05:00AM
(#35019683)
If you typed the array as string[], it would generally work, but then the compiler wouldn't be able to warn you that users.includes("matt") is always true. And it wouldn't be able to warn you that users.includes("bryan") is always false.
I could at least understand the use case for a narrow includes if you explicitly typed the array as `("matt"|"sofia"|"waqas")[]`. But that's not the type of the array. The type is `readonly ["matt", "sofia", "waqas"]`. There's always exactly one of each. There is never a reason to feed a string of type `"matt"|"sofia"|"waqas"` into includes. The only sensible parameters for includes are types that intersect with `"matt"|"sofia"|"waqas"` but can be other things too. Which means the most sensible parameter type to derive is `string`.
That logic only applies to searching for a constant, and in that case it should go a step further and complain that you're using .includes at all. There's no reason to search for the constant "bryan" or the constant "matt".
The only time it makes sense to use .includes on this array is with a string of [semi-]unknown contents. And yet that's what gets blocked. The typing is wrong.