Skip to content

Type guard functions seem to only set the type when used directly #10172

Closed
@jvanoostveen

Description

@jvanoostveen

TypeScript Version: typescript@beta (2.0)

Code

// Setup
interface A {
  value: string;
}
interface B {
  property: string;
}
type AorB = A | B;

let isB = (obj: any): obj is B => {
  return obj.hasOwnProperty('property');
};
// Setup end


let output = (obj: AorB) => {
  if (isB(obj)) {
    console.log(obj.property); // <-- works

    let values = (new Array(100)).map(a => obj.property); // <-- Property 'property' does not exist on type 'A | B'
    console.log(values);
  }
};


let someObj: B = { property: 'value' };
output(someObj);

Expected behavior:
obj.property within the map should still know its type. This works fine in pre-2.0.

Actual behavior:
The compiler does not recognise property as part of obj within the map.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions