Skip to content

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

Closed
@Rugvip

Description

@Rugvip

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.

Activity

Urtgard

Urtgard commented on Sep 9, 2022

@Urtgard

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.

added this to the Backlog milestone on Sep 9, 2022
Andarist

Andarist commented on Sep 17, 2022

@Andarist
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

nix6839 commented on Oct 24, 2022

@nix6839
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Participants

      @Rugvip@jakebailey@Andarist@Urtgard@nix6839

      Issue actions

        Type inference fails with nested arrow function callback with block Β· Issue #50687 Β· microsoft/TypeScript