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

Type inference fails with nested arrow function callback with block #50687

Closed
Rugvip opened this issue Sep 8, 2022 · 3 comments Β· Fixed by #50903
Closed

Type inference fails with nested arrow function callback with block #50687

Rugvip opened this issue Sep 8, 2022 · 3 comments Β· Fixed by #50903
Labels
Bug A bug in TypeScript
Milestone

Comments

@Rugvip
Copy link

Rugvip commented Sep 8, 2022

Bug Report

πŸ”Ž Search Terms

callback, block, inference, double

πŸ•— Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about arrow functions

⏯ Playground Link

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABAJwKYAdlwDwBUB8AFAPoQLAwDmAXIgN4BQii6AhsqwLYDOtuA3E0QRWAG1EAjVhADWtQmw48+ASkQBefIgXsuvRLjWbEYEJwmpkggL4raANzgwAJvRSooIZEmsMGaTDhCRmZFPVoARgAaIRFxKVl5Iy06d09vRFYNLSyAakQIxGsoxAB6UsRwGTA4AHcwBltBAKxgoTDlApjmOMlpOURiCWy3NHSkLOM8gqKS8sQEUQBPRGcYYGBLVEhURBhuRCgAC12qGrRXDsaVZoxWkJZdTujYsT7E7WTMkenosorFis1hstjs9gdjrtRP0FsBEAADVJjLxIAB06KK8Ou-CAA

πŸ’» Code

function repro<T>(_config: {
  params: T;
  callback: (params: T) => (params: T) => number;
}): void { return }

repro({
  params: 1,
  callback: () => { return a => a + 1 }, // a is unknown
});
repro({
  params: 1,
  callback: _b => { return a => a + 1 }, // a is a number, only difference is the ignored outer param
});
repro({
  params: 1,
  callback: () => a => a + 1, // a is a number, only difference is the lack of `{ return ... }`
});

πŸ™ Actual behavior

The type of a ends up being unknown, but only if no parameter is declared and a block is used in the outer callback.

πŸ™‚ Expected behavior

The type of the inner callback parameter a should be known in all 3 cases.

@Urtgard
Copy link

Urtgard commented Sep 9, 2022

In my project I stumbled over a similiar case:

class Test {
  array = [1, 2, 3]
  optional?: number

  public methodError(): void {
    if (!this.optional) return;

    this.array.forEach((x) => {
      x = this.optional; // Type 'number | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'.(2322)
    });
  }

  public methodOk(): void {
    this.array.forEach((x) => {
      if (!this.optional) return;
      x = this.optional;
    });
  }
}

Let me know if this is different problem, then I'll open a new issue.

@jakebailey jakebailey added the Bug A bug in TypeScript label Sep 9, 2022
@jakebailey jakebailey added this to the Backlog milestone Sep 9, 2022
@Andarist
Copy link
Contributor

@Urtgard your problem is different and it's a not a bug, take a look at the inline explanation that I have added in this TS playground

@nix6839
Copy link

nix6839 commented Oct 24, 2022

function fn<ToInferred>(
  callback: (unused: string) => {
    key: ToInferred;
    keyCallback: (key: ToInferred) => number;
  },
) {}

fn(() => ({
  key: 5,
  keyCallback: (key) => {
    return key;
  },
}));

fn((unused) => ({
  key: 5,
  // @ts-expect-error key is unknown
  keyCallback: (key) => {
    return key;
  },
}));

Same issue here... I hope it will be fixed at v4.9.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants