Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@domoritz

Description

@domoritz

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`
}

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions