Recently delving into Typescript and following along with an educational video. Encountered a strange behavior that seems like a bug. For instance:
const json = '{"x": 10, "y":10}';
const coordinates: { x: number; y: number } = JSON.parse(json);
console.log(typeof coordinates.y);
In usual cases, this should display x and y as type numbers. However, the discrepancy is not due to the type declaration but rather the JSON value. If one of them is declared as a string, VS Code interprets it as such although internally it remains a number:
const json = '{"x": 10, "y":10}';
const coordinates: { x: number; y: string } = JSON.parse(json);
console.log(typeof coordinates.y);
Here's the code snippet:
https://i.sstatic.net/PnFrM.png
As shown above, VS Code sees it as a string.
https://i.sstatic.net/vj0wt.png
Nevertheless, the type checking indicates otherwise.
In my view, it would make sense for an Object/Array post-parsing to have any type. However, there should be a clear notification of error if the parsed JSON value does not align with your annotation, or it should alter the value to match the annotation accurately.
I'm still a newbie at this, so please correct me if I'm mistaken!