Skip to content

Type inference for piped arrow functions #22081

Closed
@ivan7237d

Description

@ivan7237d

TypeScript Version: 2.7.1 (the issue occurs independently of whether the strict mode, including strictFunctionTypes, is turned on)

Search Terms: pipe, type inference, generic

Code/actual behavior:

const pipe = <A, B, C>(
  x: A,
  a: (x: A) => B, 
  b: (x: B) => C,
) => b(a(x));

// This just calls the function passed as argument. In
// other words, it's an identity function that takes
// an argument of a specific type (x: A) => B.
const call = <A, B>(f: (x: A) => B) => (x: A) => f(x)

// For this line, TS can't infer type of the last argument
// and addition produces error.
const a = pipe(1, x => x + 1, call(x => x + 1));
// But it successfully infers types for this line where
// the second argument is wrapped in call(...)
const b = pipe(1, call(x => x + 1), call(x => x + 1));

Expected behavior:
Typescript should be able to infer the types for the simpler one of the these two lines if it can infer them for the more complex one.

Playground Link:
Playground

Related Issues:
This issue originates from discussion here #22051.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions