Skip to content

Commit

Permalink
fix: NumberType to handle any and union
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Mar 20, 2023
1 parent bc82e90 commit 23fd42a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-hats-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'type-plus': patch
---

Fix `NumberType` to handle `any` and union.
3 changes: 2 additions & 1 deletion ts/number_plus/number.is_positive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ it('returns false if N is number as it can include negative', () => {
isType.equal<true, IsPositive<number>, false>()
})

it('returns false if N is never', () => {
it('returns false if N is never or any', () => {
isType.equal<true, IsPositive<never>, false>()
isType.equal<true, IsPositive<any>, false>()
})

it('returns true for 0', () => {
Expand Down
9 changes: 9 additions & 0 deletions ts/number_plus/number.number_type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ it('returns number for type number', () => {

it('returns never for never', () => {
isType.never<NumberType<never>>()
isType.never<NumberType<any>>()
})

it('returns never if N is union of non number', () => {
isType.never<NumberType<number | string>>()
})

it('returns number if N is union of number and number literal', () => {
isType.equal<true, number, NumberType<number | 1>>()
})

it('returns never for numeric literals', () => {
Expand Down
3 changes: 2 additions & 1 deletion ts/number_plus/number.positive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ it('returns never if N is number as it can include negative', () => {
isType.never<Positive<number>>()
})

it('returns never if N is never', () => {
it('returns never if N is never or any', () => {
isType.never<Positive<never>>()
isType.never<Positive<any>>()
})

it('returns 0 if N is 0', () => {
Expand Down
7 changes: 6 additions & 1 deletion ts/number_plus/number.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IsNever } from '../PrimitiveTypes.js'
import type { IsAny } from '../any_plus/any.js'

/**
* Either number or bigint.
Expand Down Expand Up @@ -105,7 +106,11 @@ export type NonNegative<N extends Numeric, Then = N, Else = never> = Negative<N,
* type R = NumberType<1> // false
* ```
*/
export type NumberType<T extends number, Then = T, Else = never> = number extends T ? Then : Else
export type NumberType<T, Then = T, Else = never> = IsAny<
T,
Else,
[number, T] extends [T, number] ? Then : Else
>

/**
* Check if the value is a positive number.
Expand Down
10 changes: 6 additions & 4 deletions ts/predicates/Equal.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnyFunction, assertType, Equal, NotEqual, ValueOf } from '../index.js'
import { AnyFunction, assertType, Equal, isType, NotEqual, ValueOf } from '../index.js'

describe('Equal<A, B>', () => {
it('compares among booleans', () => {
Expand Down Expand Up @@ -119,9 +119,11 @@ describe('Equal<A, B>', () => {
})

it('works against any', () => {
assertType.isTrue(true as Equal<any, any>)
assertType.isFalse(false as Equal<any, 1>)
assertType.isFalse(false as Equal<1, any>)
isType.equal<true, true, Equal<any, any>>()
isType.equal<true, false, Equal<any, 1>>()
isType.equal<true, false, Equal<1, any>>()

// isType.equal<true, false, Equal<[any], [1]>>()
})

it('works against function overload', () => {
Expand Down

0 comments on commit 23fd42a

Please sign in to comment.