microsoft / microsoft/TypeScript

ts2367 Comparing non-comparable types not detected with optional property

Open
#32,627 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproduction in functions x, y, and z, or open the linked TypeScript Playground. Compare the diagnostics for required, explicitly undefined, and optional properties. Done means the comparison in z reports TS2367 like x and y, with existing behavior preserved for the other examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.