Consider this scenario: the code snippet below will result in an Object is of type 'unknown'.
error:
let obj: {[key: string]: unknown} = {hello: ["world", "!"]}; // Could be anything with the same structure
let key = "hello"; // Any key from obj
if (Array.isArray(obj[key])) obj[key][1] = obj[key][0];
However, this next piece of code does not trigger the error:
let obj: {[key: string]: unknown} = {hello: ["world", "!"]}; // Could be anything with the same structure
let key = "hello"; // Any key from obj
let array = obj[key];
if (Array.isArray(array)) array[1] = array[0];
Is this expected behavior, or could it be a bug?
One possible explanation could involve a getter that returns different values each time:
const values = ["hello", 16, true];
let obj: {x: number, [key: string]: unknown} = {
x: 0,
get y() {
return values[this.x = (this.x + 1) % values.length];
}
};
Should we be concerned about this?