I'm currently delving into Typescript and I find myself in a bit of a quandary as to why this particular code manages to compile successfully. In the given code snippet, images
is supposed to be an Array of strings, yet when it comes to assigning values to this.data
, 'images' suddenly transforms into an Object.
declare type vd = {
images: Array<string>;
}
class mytest {
data: vd;
constructor() {
let obj = ['hello!', 'bye'];
this.data = { images: { ...obj } };
console.log(this.data.images);
}
}
Unsurprisingly, the following output appears on the console:
Object 0: "hello!" 1: "bye"
If we switch out the curly braces for regular parentheses, then the output changes to:
Array(2) 0: "hello!" 1:"bye" length: 2
Why doesn't the compiler raise any objections regarding the assignment of this.data
in the initial scenario?