sindresorhus / type-fest

A collection of essential TypeScript types
Creative Commons Zero v1.0 Universal
14.42k stars 548 forks source link

Can i use GreaterThan as parameter type? #957

Open enccode opened 2 months ago

enccode commented 2 months ago

Newbie here...

Is it possible to use GreaterThan type for the function/method parameters?

when I write the following function:

import type {GreaterThan} from 'type-fest'

function printNumberOfBooks <N extends number> (numberOfBooks: GreaterThan<N, 0>): void {
    console.log(numberOfBooks)
}

and... :

printNumberOfBooks(2)

I get the following typescript error:

Argument of type 'number' is not assignable to parameter of type 'never'

It took me hours... I didn't find a solution

Upvote & Fund

Fund with Polar

sindresorhus commented 1 month ago

Answer here: https://www.reddit.com/r/typescript/comments/1fs64kb/can_i_use_greaterthan_as_parameter_type/

Would be nice to include an example of this in the doc comment of the type.

enccode commented 1 month ago

I asked this question 2 hours ago on reddit...came to post the link here and saw you did it faster... I would be happy if I could help you by putting the examples in the documentation... but I think it's better if someone with more experience in TypeScript does it... Thank you for your time and making very useful tools.

ikelax commented 1 month ago

Answer here: reddit.com/r/typescript/comments/1fs64kb/can_i_use_greaterthan_as_parameter_type

Would be nice to include an example of this in the doc comment of the type.

There are already examples in the doc comment of GreaterThan.