sindresorhus / is

Type check values
MIT License
1.68k stars 109 forks source link

Improve `is.integer`, `is.safeInteger`, `is.infinite` TypeScript type #152

Closed younho9 closed 2 years ago

younho9 commented 2 years ago
export type Integer<Type> = Type extends number ?
    `${Type}` extends `${bigint}`
        ? Type
        : never
    : never;

resolve #147

sindresorhus commented 2 years ago

Why are they broken by this change and why would unknown fix it?

younho9 commented 2 years ago

Parameter type of is.integer is narrowed to number, so is.integer does not extend Predicate type. is.integer is used in test cases of is.any which require Predicate type function, so broken.

Should I just delete these test cases?

https://github.com/sindresorhus/is/blob/main/test/test.ts#L1528 https://github.com/sindresorhus/is/blob/main/test/test.ts#L1553

sindresorhus commented 2 years ago

is.integer() should still accept unknown, like all the other value checks. If it's unknown, I guess the type guard can simply be number. I don't think we should change the Integer type for this though.

younho9 commented 2 years ago

is.integer() should still accept unknown, like all the other value checks. If it's unknown, I guess the type guard can simply be number. I don't think we should change the Integer type for this though.

I agree with your opinion.

sindresorhus commented 2 years ago

Why did you close?

younho9 commented 2 years ago

Why did you close?

I think there are some limitation without changing Integer type.

<T>(value: T): value is Integer<T> => Number.isInteger(value);
// => Type 'T' does not satisfy the constraint 'number'
sindresorhus commented 2 years ago

Can't you use an overload?

younho9 commented 2 years ago

Unfortunately, inference from a function will only try the last overload signature.

TypeScript Playground

sindresorhus commented 2 years ago

Is this a TypeScript bug/limitation or intentional behavior? If it's a bug/limitation, we could potentially wait for it to be fixed.

sindresorhus commented 2 years ago

Could we do something like this? (Not working)

https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBDAnmYcCSA7GwDmwoA8AKsqqNhgCYDOcGArgLYBG+AfHALxwAGAJAG8SKAL4845YFVr8BzAJY55WMXAD8cYagBcdYADd8AbgCwAKHOhIsBKXRZc+APJQAqhgDWGCAHcMxUg5uLQkQCho6JlYodXtsPEItDl0GFmNzcwAzegwAYxh5CAw4eWpMePxiUPDaVOi4AB84HK9fDDYACn0AQwAbemBdIgBKXR7+1FK4xygXd1a-Yg4BcwBIKGAYeihigDko-AA6UvKZrr6B4dMzEQyzSmBc3u6NuFyi6ngGXufmXuB9mkoCkDjEmt9etdzPJMh0Tg4Eh0Id0-gDQcNhnAVmY4LjIj8Uf9AdFzCIgA

younho9 commented 2 years ago

Is this a TypeScript bug/limitation or intentional behavior? If it's a bug/limitation, we could potentially wait for it to be fixed.

It seems a design limitation. https://github.com/microsoft/TypeScript/issues/45634#issuecomment-912820467

Could we do something like this? (Not working)

typescriptlang.org/play?#code/KYDwDg9gTgLgBDAnmYcCSA7GwDmwoA8AKsqqNhgCYDOcGArgLYBG+AfHALxwAGAJAG8SKAL4845YFVr8BzAJY55WMXAD8cYagBcdYADd8AbgCwAKHOhIsBKXRZc+APJQAqhgDWGCAHcMxUg5uLQkQCho6JlYodXtsPEItDl0GFmNzcwAzegwAYxh5CAw4eWpMePxiUPDaVOi4AB84HK9fDDYACn0AQwAbemBdIgBKXR7+1FK4xygXd1a-Yg4BcwBIKGAYeihigDko-AA6UvKZrr6B4dMzEQyzSmBc3u6NuFyi6ngGXufmXuB9mkoCkDjEmt9etdzPJMh0Tg4Eh0Id0-gDQcNhnAVmY4LjIj8Uf9AdFzCIgA

In this example, if value type is string or something, signature could be function isInteger<string>(value: string): value is number which return type doesn't match with input type.

sindresorhus commented 2 years ago

@younho9 Is there any way we can move this forward or should I close?

younho9 commented 2 years ago

I couldn't find a way to solve it. I think you'd better close.