ThomasReggi
ThomasReggi

Reputation: 59345

Type function with generic callbacks that match

const example = <a, b> (o: (a) => b, c: (b) => a) => {

}

example(() => 1, (n) => n)

I'm looking for n to be assigned to number, but I get no warning.

Upvotes: 0

Views: 47

Answers (1)

Kryten
Kryten

Reputation: 15750

Playing with this in the Typescript Playground, I came up with the following that seems to resolve the types the way you expect:

const example = <a, b>(o: ((arg1: a) => b), c: ((arg2: b) => a)) => {}

example(() => 1, (n) => n);

There are two differences here:

  1. I wrapped the example function argument types in parentheses to make them a little clearer (at least in my mind - I was having trouble wrapping my head around functions inside functions), and
  2. I added argument names (arg1 and arg2)

I'm reasonably sure that this is what you intended - I believe that a function type expression requires argument names. For example, the following results in a "Parameter has a name but no type. Did you mean 'arg0: a'?" error in the playground:

type f1 <a, b>(a) => b;

Upvotes: 2

Related Questions