microsoft / microsoft/TypeScript
ts2367 Comparing non-comparable types not detected with optional property
Nobody has claimed this yet.
- 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.
Related Issues: n/a
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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