Skip to content

Return type not inferred correctly in static method #7497

Closed
@SylvainEstevez

Description

@SylvainEstevez

TypeScript Version:
1.8.7

Code

'use strict';

class User {
  firstName: string;
  lastName: string;  

  static getInstance() {
    return new this();
  }
}

class StaffUser extends User {  
  position: string;
}

let staffUser = StaffUser.getInstance();

console.log(staffUser.position); // error TS2339: Property 'position' does not exist on type 'User'

Expected behavior:

staffUser should be of type StaffUser, not simply User.

Actual behavior:
The compiler doesn't guess that getInstance in StaffUser returns instances of StaffUser.

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