Why is the following code snippet not raising any errors or warnings?
{
let x: number;
let y: any;
y = "hello";
x = y;
}
My expectations are: anything should be assigned to y
as it is explicitly typed as any
, but only numbers should be assigned to x
as it is explicitly typed as number
and if an any
needs to be assigned to x
then that should be explicitly cast to number
:
x = y as number;
How could the current behaviour considered to be acceptable or as a good idea and not a bug in the first place? Moreover, is there a way to force the compiler to raise an issue about this and only accept explicit casting?
Extra Info:
tsc
version:3.2.2
- compiler options:
{ "target": "ES5", "noImplicitAny": true, "strictNullChecks": true, "downlevelIteration": true, "experimentalDecorators": true, "strictBindCallApply": true, "strictFunctionTypes": true, "strictPropertyInitialization": true }