Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript Fails to Infer Specific Generic Types as Expected #58193

Closed
unclesmiley opened this issue Apr 15, 2024 · 2 comments
Closed

TypeScript Fails to Infer Specific Generic Types as Expected #58193

unclesmiley opened this issue Apr 15, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@unclesmiley
Copy link

I'm currently facing an issue with TypeScript where it seems to be unable to correctly infer types in a specific scenario involving generics, enums, and inheritance.

Here is a simplified version of my code:

enum Status {
  UNDEFINED = 0,
  SUCCESS = 1,
  FAIL = 2,
}

class Result<StatusT extends Status = Status, Data = unknown, Code = string | number> {
  constructor(
    public status?: StatusT,
    public data?: Data,
    public code?: Code,
  ) {}
}

abstract class Task {
  async done(...args: any[]) {
    while (true) {
      const result = await this.execute(...args);
      if (result.status === Status.SUCCESS) {
        return result;
      }
    }
  }

  abstract execute(...args: any[]): Promise<{
    [K in Status]: Result<K>;
  }[Status]>
}

class TestTask extends Task {
  // (method) TestTask.execute(): Promise<Result<Status.SUCCESS, {}, number> | Result<Status.FAIL, string, string | number>>
  async execute () {
    if (Math.random() > 0.5) {
      return new Result(Status.SUCCESS, {}, 0);
    } else {
      return new Result(Status.FAIL, "error");
    }
  }
}

const test = new TestTask()

// got: `Promise<Result<Status.SUCCESS, unknown, string | number>>`
// but i want: `Promise<Result<Status.SUCCESS, {}, number>>`
test.done();

TypeScript only infers the return type of done() as Promise<Result<Status.SUCCESS, unknown, string | number>>, while I expect it to infer Promise<Result<Status.SUCCESS, {}, number>>.

Why does TypeScript fail to infer the specific generic types as I expect in the TestTask class? Is this a limitation of TypeScript's type inference system, or am I missing something in how I should annotate or structure my types or code?

@unclesmiley unclesmiley added the Duplicate An existing issue was already created label Apr 15, 2024
@jcalz
Copy link
Contributor

jcalz commented Apr 15, 2024

This isn't an appropriate place to ask this question, and you haven't properly filled out the issue template. Please close this to spare the TS team from having to do so. Please stick to Stack Overflow or some other more appropriate place if you're asking questions.

@RyanCavanaugh
Copy link
Member

To expedite the triage process, we need everyone to follow the issue template and instructions.

When you clicked "Create New Issue", the issue form was pre-populated with a template and some instructions. We need you to read those instructions completely, follow them, and then fill in all the fields in that template.

We are not able to assist with issues that don't follow the template instructions as they represent a significantly larger amount of work compared to issues which are correctly specified. Thank you for understanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants