Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts2367 Comparing non-comparable types not detected with optional property #32627

Open
junoatwork opened this issue Jul 30, 2019 · 2 comments
Open
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@junoatwork
Copy link

TypeScript Version: 3.6.0-dev.20190730 (also with 3.5.1)

Search Terms: compare types, comparable, always false comparison, optional property, properties

Code

enum X {
  A= 'A'
}

function x(res: {action: string}):boolean {
  return res === X.A // error
}

function y(res: {action: string | undefined}):boolean {
  return res === X.A // error
}

function z(res: {action?: string}):boolean {
  return res === X.A // no error, but always false
}

function correct(res: {action: string}):boolean {
  return res.action === X.A // what the code should have been
}

Expected behavior:

The comparison in function z should have resulted in error TS2367: This condition will always return 'false' since the types '{ action?: string; }' and 'X.A' have no overlap.

Actual behavior:

Functions x, y give the correct error. Function z has no compiler error, but the condition will always evaluate to false since it is comparing an object and a string.

Playground Link: https://www.typescriptlang.org/play/#code/KYOwrgtgBAGlDeBYAUFNUCCBeKByDuKAviigGZggDGALgJYD2IUAHgBQBOwAzgFwIBDWoxD9uNDnRABzIgEpeAIwYMANsAHMkyLjTAdmXblCynYAOgxQA9NajAOHBh2KlkFavSZQAnpx788EJeolDikjJQAD5QlAAmwGRSwHHySirqmggouvqGPCZmMJY2dg5OLsgkyOSUwt4AXv58gvUgAPxiElKyCspqGlo5wHoGUEaFOMVWtlAgDPaOzgA0UIpgNFACqgDuAj7GZNvcwK417nUhUFTOXLTNgcEiXRG96QNZ2rljRuZP3qYpiVZjsABYCTY0UHAa4MBJhUEMMCqOJQcEANxhimAoDOQA

Related Issues: n/a

@DanielRosenwasser
Copy link
Member

It's an interesting question as to whether or not comparability checks should actually be exempt from weak type checks. I'm usually pretty conservative on this stuff, but my intuition tells me this would not break that much.

@haymez
Copy link

haymez commented Aug 29, 2022

I recently ran across some broken code in a repo that lead me here. Basically my situation was something like the following:

interface Car {
  make: string
  model: string
}

type SomeType = Partial<Car>

const car: SomeType = {}

if (car === "typescript doesn't complain about this") {
    // This code will never execute..?
}

Weak typing is clearly giving us a false sense of security about this code by not warning us that there's no overlap between the partial type and a string. Would love for Typescript to detect these types of errors in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
Development

No branches or pull requests

3 participants