Ts-toolbelt: How to use this module?

Created on 19 Aug 2019  路  7Comments  路  Source: millsp/ts-toolbelt

馃 Question

How to use this module? Is it possible used with ramda?

Describe your question

Q1: How to simply create a type for a curry function?

Like this add function

const add = (x: number) => (y: number) => x + y

I know we can create a function type and turn it to curry.

type add = F.Function<[ number, number], number>
declare function curry<Fns extends F.Function>(fns: Fns): F.Curry<Fns>
const add = (x: number, y: number): number => x + y

const addCurry = curry(add)
const c = addCurry(1)(2)

Is there a more simply way?

Q2: Is this module work with ramda?

ramda curry type is not so friendly in typescript

R.map(x => x + 1)([1,2,3]) // error, x: {}, Operator '+' cannot be applied to types '{}' and '1'

Is there a way to change ramda function type use this module?

Thanks

limitation question

Most helpful comment

Unfortunately, the type inference cannot work this way when it is curried. And there's also a few limitations with currying and generics (which should solve in the future https://github.com/microsoft/TypeScript/issues/5453).

All 7 comments

The curry types from this library are already shipped with ramda. Make sure you have TS >= 3.5 and the latest @types/ramda in your package.json :)

const curry = <Fn extends F.Function>(fn: Fn): F.Curry<Fn> => {
   // your curry stuff
}

@pirix-gh Thanks a lot for your reply.

I test ramda map curry function, It is always error with x unknown.

@types/ramda is lastest version

dependencies
"@types/ramda": "^0.26.19"
"typescript": "^3.5.3"
"ramda": "^0.26.1"

CleanShot 2019-08-20 at 00 48 38

I have look into @types/ramda map's type, it look right. Is it typescript limit? Or there is some thing can workaround?

CleanShot 2019-08-20 at 00 52 09

You must be new to typescript. I guess that you missed out:

R.map((x: number) => x + 1)([1,2,3])

But this is not related to this library ;)

Cheers

@pirix-gh Ya, I just want to can I use ts-toolbelt let typescript know the x type in curry function automatically .
Because this work

R.map(x => x + 1, [1,2,3]); // x is number auto

Unfortunately, the type inference cannot work this way when it is curried. And there's also a few limitations with currying and generics (which should solve in the future https://github.com/microsoft/TypeScript/issues/5453).

@pirix-gh Thank you so much! I get it. 馃榾

Was this page helpful?
0 / 5 - 0 ratings