Skip to content

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

Closed

Description

@andropov-sergej

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:

Activity

  1. jcalz commented on Feb 4, 2020

    @jcalz
    Contributor

    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,

  2. typescript-bot commented on Feb 11, 2020

    @typescript-bot
    Contributor

    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.

  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions