Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Type guards don't work for interfaces with only optional properties #5842

Copy link
Copy link
@domoritz

Description

@domoritz
Issue body actions

For some reason type guards to not work if an interface has only optional properties.

interface A {
  foo : number;
}

function isA(object: any): object is A {
  return true;
}

let a: A | boolean;

if (isA(a)) {
  a.foo;   // compiles
}



interface B {
  foo? : number;
}

function isB(object: any): object is B {
  return true;
}

let b: B | boolean;

if (isB(b)) {
  b.foo;  // does not compile because type inference says its `B | boolean`
}
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.