Upon reviewing some old code that I wrote, I realized that I had neglected to specify a generic type for a Set
. The type was set as Set<unknown>
. Strangely, despite this, I found that I could still utilize all the methods of the Set
without encountering any errors. This stands in contrast to untyped arrays, which would result in errors. For example:
const set = new Set();
set.has(1);
const map = new Map();
map.has(1);
const arr = []; // Variable 'arr' implicitly has type 'any[]' in some locations where its type cannot be determined.
arr.includes(1); // Variable 'arr' implicitly has an 'any[]' type.
I am now wondering how I can modify the behavior of Set and Map to also throw errors when their types are unknown.