Typescript has pretty cool error message on checking different types:
let strange_boolean = true;
let strange_string: string = "1";
console.log(strange_boolean == strange_string);
error: TS2367 [ERROR]: This condition will always return 'false' since the types 'boolean' and 'string' have no overlap.
console.log(strange_boolean == strange_string);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For but this code compiles:
let strange_boolean: any = true;
let strange_string: string = "1";
console.log(strange_boolean == strange_string);
Because any
can be converted to anything
...
But what if I want somehow to disable even implicit conversion from any
to anything ?
Is Typescript
has any flag for that ?
I want that this code compiles only in that case:
let strange_boolean: any = true;
let strange_string: string = "1";
console.log(String(strange_boolean) == strange_string);
or
let strange_boolean: any = true;
let strange_string: string = "1";
console.log(Boolean(strange_boolean) == Boolean(strange_string));
Unfortunately there is no such possibility at the current moment ... (
I have created issue https://github.com/microsoft/TypeScript/issues/39209, but it was closed, because Typescript team do not want break backward comparability for any
and it is unfortunate, because if they will add this flag then code become more safe than in "Vanilla" JavaScript