0

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));
Denis Kotov
  • 735
  • 7
  • 22
  • I've not written much TypeScript lately, but maybe the `unknown` type helps here? – Joachim Sauer Jun 22 '20 at 20:49
  • @JoachimSauer What is unknown type ... and actually it works even for this strange type ... But I want to disable working code like any == string ... I want only explicit actions, explicit conversions – Denis Kotov Jun 22 '20 at 20:51
  • 1
    Since `==` is reference comparison I don't think there's a straightforward way to achieve what you want, but `unknown` is similar to `any` except that while `any` is "this could be anything so you can do whatever with it", `unknown` is more "this could be anything so you need to check what it is before doing anything with it". – Joachim Sauer Jun 22 '20 at 20:53
  • 1
    Related: ['unknown' vs. 'any'](https://stackoverflow.com/q/51439843) – VLAZ Jun 22 '20 at 20:55
  • @JoachimSauer I just want to prevent implicit conversions ;) `==` it is reference comparison only in case if it is the same object, but what if it is another object ? I want at compile time prevent such comparasion with comile error – Denis Kotov Jun 22 '20 at 20:56
  • 1
    The TS compiler doesn't do this for you, but if you use a linter like ESLint you could use a rule like [`no-explicit-any`](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-explicit-any.md). Does that answer your question? – jcalz Jun 23 '20 at 01:56
  • Possible duplicate of [Is this a TypeScript Type System Bug?](https://stackoverflow.com/questions/59313928/is-this-a-typescript-type-system-bug) – jcalz Jun 23 '20 at 01:58

1 Answers1

0

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

Denis Kotov
  • 735
  • 7
  • 22