Recently, I came across this interesting function:
function fn(param: Record<string, unknown>) {
//...
}
x({ hello: "world" }); // Everything runs smoothly
x(["hi"]); // Error -> Index signature for type 'string' is missing in type 'string[]'
It is logical because the Index Signature
in an Array
is typically a Number
, whereas in a Record
it is defined as a string
.
However,
If I replace unknown
with any
, like so:
function fn(param: Record<string, any>) {
//...
}
x({ hello: "world" }); // No issues
x(["hi"]); // Works perfectly fine now
The Question Arises: Even though only the Value
has been changed to Any
and not the Key
type in Record
, why am I not encountering any errors despite the Index Signature
for arrays being Number
and for records being string
?