Skip to content

Error is not showing on comparing when interface has optional variables #36602

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

Closed
andropov-sergej opened this issue Feb 4, 2020 · 2 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@andropov-sergej
Copy link

andropov-sergej commented Feb 4, 2020

Code

interface A {
    id?: number;
}
const a:A = {
    id: 5
};
const testA: boolean = (a === 1); //works, but why?


interface B {
    id: number;
}
const b:B = {
    id: 5
};
const testB: boolean = (b === 1); //compiler error -> expected that also on A

Expected behavior:
An error on the compare line.

Actual behavior:
No error

Playground Link:
https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=18&pc=1#code/JYOwLgpgTgZghgYwgAgILIN4Chm+cAEwH4AuZEAVwFsAjaAbiwF8sEB7EAZzGThPQC8mHHkJkArM0bsuPSN1RkabNgBsIcEMiEAKONoFCAjAEp6yAPQWA7mygBrTgBpkNCj2sALAJ5Es-0EhYRBQAIWE8fAIySloGZlYOblcScKFsSLFkSSZpJLkIblClFXVNbWQdGgNjM0sLdioAB2B1KGRoKDtkAFoAPg6ADyaIBEgCZDBPOB44VU42ZA40fyA

Related Issues:

@jcalz
Copy link
Contributor

jcalz commented Feb 4, 2020

Context: relevant SO question

Search terms: weak type detection, comparison operators

Related issues:
#16047 - weak type detection PR
#17215 - comparability spec PR that I think never made it anywhere.

Looks like this behavior is intentional, at least if I can take #17215 for its word:

whether a type is considered weak has no bearing on whether two types are comparable,

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Feb 6, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants