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

Two generic functions and composition #39644

Closed
polemius opened this issue Jul 17, 2020 · 2 comments
Closed

Two generic functions and composition #39644

polemius opened this issue Jul 17, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@polemius
Copy link

TypeScript Version: 3.9.2

Search Terms: two generic functions composition

Code

function id<T>(x: T): T {
  return x
}

function composition<A, B, C>(
  f: (p1: A) => B,
  g: (p2: B) => C,
): (p: A) => C {
  return (x: A) => g(f(x))
}

const a = (x: number) => x + 1
const comp = composition(a, id)
// Error
// Argument of type '<T>(x: T) => T' is not assignable to parameter of type '(p1: unknown) => number'
const comp2 = composition(id, a)

Expected behavior:

No errors shows.
The type of function comp2 should be (p: number) => number.

Actual behavior:

Type of function comp is (p: number) => number which is correct.
But type of function comp2 is (p: never) => number which is not correct.
Also TypeScript shows the error: Argument of type '<T>(x: T) => T' is not assignable to parameter of type '(p1: unknown) => number'

Playground Link: Link

@polemius polemius changed the title Two generic functions Two generic functions and composition Jul 17, 2020
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jul 17, 2020
@ahejlsberg
Copy link
Member

This is effectively a duplicate of #30727. As I comment here, it's not immediately clear how we could do better given the left-to-right nature of our type inference algorithm. It's one of those examples that require full unification to make the correct inferences.

@ahejlsberg ahejlsberg added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Jul 21, 2020
@ahejlsberg ahejlsberg removed their assignment Jul 21, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

4 participants