Amit Erandole
Amit Erandole

Reputation: 12281

How do I assign the right type signature to this curried function in typescript?

I am new to typescript & generics and working with a Maybe monad. I have created this small utility function using Ramda:

const fromEmpty = R.ifElse(hasLength, Just, Maybe.zero);

The closest signature I can infer from this is:

type EmptyFunc<T> = (val: T[]) => Maybe<T[]>;

It's a function takes in an array and returns a function that returns a Maybe of that array.

I tried doing

const fromEmpty(<U extends EmptyFunc<U>) = R.ifElse(hasLength, Just, Maybe.zero);

But that doesn't work. It returns error TS1005: ',' expected.

What is the right way to work with curried functions in typescript?

Upvotes: 0

Views: 313

Answers (1)

Matt McCutchen
Matt McCutchen

Reputation: 30929

The declaration

type EmptyFunc<T> = (val: T[]) => Maybe<T[]>;

declares a family of different function types EmptyFunc<number>, EmptyFunc<string>, etc., each of which works for only the specified type T. You probably meant

type EmptyFunc = <T>(val: T[]) => Maybe<T[]>;

which declares the type EmptyFunc of a single generic function that works for all types T. Then to apply this type to fromEmpty, just write:

const fromEmpty: EmptyFunc = R.ifElse(hasLength, Just, Maybe.zero);

(I'm unable to test this myself since you didn't give the definitions of hasLength, Just, and Maybe.) If that wasn't what you were asking, please clarify the question.

Upvotes: 1

Related Questions