Looking at the following typescript code snippet:
let a;
a = "number";
let t = a.endsWith('r');
console.log(t);
It is worth noting that since variable 'a' is not declared with a specific type, the compiler infers it as an 'any' type. Despite this, when we assign a string value to 'a' and use the 'endsWith' function against it, there is no compile error thrown by the compiler. This is intriguing because 'endsWith' is not a valid function for an 'any' type. Nevertheless, the code still compiles/transpiles into JavaScript successfully and executes without any issues.
The correct way to write this code would be:
let a : string;
a = "number";
let t = a.endsWith('r');
console.log(t);
However, it raises the question of why the previous code block compiles without errors?