Skip to content

Commit

Permalink
fix: partial customize for isAny
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Sep 24, 2023
1 parent d0a916f commit 6a0481d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 54 deletions.
8 changes: 8 additions & 0 deletions type-plus/ts/any/is_any.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ it('as branching', () => {
testType.equal<IsAny<never, $SelectionBranch>, $Else>(true)
testType.equal<IsAny<void, $SelectionBranch>, $Else>(true)
})

it('works with partial customization', () => {
testType.equal<IsAny<any, { $then: 1 }>, 1>(true)
testType.equal<IsAny<0, { $then: 1 }>, false>(true)

testType.equal<IsAny<any, { $else: 2 }>, true>(true)
testType.equal<IsAny<0, { $else: 2 }>, 2>(true)
})
3 changes: 2 additions & 1 deletion type-plus/ts/any/is_any.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { $SelectionOptions, $SelectionPredicate } from '../type_plus/branch/selection.js'
import type { $ResolveOptions } from '../type_plus/resolve_options.js'

/**
* 🎭 *predicate*
Expand All @@ -24,4 +25,4 @@ import type { $SelectionOptions, $SelectionPredicate } from '../type_plus/branch
export type IsAny<
T,
$O extends $SelectionOptions = $SelectionPredicate
> = 0 extends 1 & T ? $O['$then'] : $O['$else']
> = 0 extends 1 & T ? $ResolveOptions<[$O['$then'], true]> : $ResolveOptions<[$O['$else'], false]>
11 changes: 7 additions & 4 deletions type-plus/ts/null/null_type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IsAny } from '../any/is_any.js'
import type { $Else, $SelectionBranch, $Then } from '../type_plus/branch/selection.js'

/**
* Check if the type `T` is exactly `null`.
Expand All @@ -11,10 +12,12 @@ import type { IsAny } from '../any/is_any.js'
* type R = NullType<string | boolean> // never
* ```
*/
export type NullType<T, Then = T, Else = never> = IsAny<T, {
$then: Else,
$else: [T, null] extends [null, T] ? Then : Else
}>
export type NullType<T, Then = T, Else = never> = IsAny<
T,
$SelectionBranch> extends infer R
? R extends $Then ? Else
: R extends $Else ? [T, null] extends [null, T] ? Then : Else
: never : never

/**
* Is the type `T` exactly `null`.
Expand Down
10 changes: 5 additions & 5 deletions type-plus/ts/number/strict_number_type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IsAny } from '../any/is_any.js'
import type { $Else, $SelectionBranch, $Then } from '../type_plus/branch/selection.js'

/**
* Check if the type `T` is exactly the type `number` and not numeric literals.
Expand All @@ -18,11 +19,10 @@ import type { IsAny } from '../any/is_any.js'
*/
export type StrictNumberType<T, Then = T, Else = never> = IsAny<
T,
{
$then: Else,
$else: [number, T] extends [T, number] ? Then : Else
}
>
$SelectionBranch> extends infer R
? R extends $Then ? Else
: R extends $Else ? [T, number] extends [number, T] ? Then : Else
: never : never

/**
* Is the type `T` exactly the type `number` and not numeric literals.
Expand Down
42 changes: 20 additions & 22 deletions type-plus/ts/numeric/negative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,25 @@ import type { Zero } from './numeric_type.js'
*/
export type Negative<T, Then = T, Else = never> = IsAny<
T,
{
$then: Then | Else,
$else: IsNever<
T,
{
$then: Else,
$else: [number, T] extends [T, number]
? Then
: [bigint, T] extends [T, bigint]
$SelectionBranch> extends infer R
? R extends $Then ? Then | Else
: R extends $Else ? (IsNever<
T,
$SelectionBranch> extends infer R2
? R2 extends $Then ? Else
: R2 extends $Else ? ([number, T] extends [T, number]
? Then
: ([bigint, T] extends [T, bigint]
? Then
: T extends Zero
? Else
: [T] extends [number | bigint]
? `${T}` extends `-${string}`
? Then
: Else
: Else
}
>
}
>
: Else))
: never : never)
: never : never

/**
* Is 'T' a negative numeric type.
Expand Down Expand Up @@ -73,12 +71,12 @@ export type IsNegative<T, Then = true, Else = false> = Negative<T, Then, Else>
*/
export type NotNegative<T, Then = T, Else = never> = IsAny<
T,
{
$then: Then | Else,
$else: IsNever<
T,
$SelectionBranch
> extends infer R
$SelectionBranch> extends infer R
? R extends $Then ? Then | Else
: R extends $Else ? (IsNever<
T,
$SelectionBranch
> extends infer R
? R extends $Then ? Then
: R extends $Else ? [number, T] extends [T, number]
? Then
Expand All @@ -90,8 +88,8 @@ export type NotNegative<T, Then = T, Else = never> = IsAny<
: Then
: Then
: never : never
}
>
)
: never : never

/**
* Is 'T' not a negative numeric type.
Expand Down
20 changes: 9 additions & 11 deletions type-plus/ts/numeric/positive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { IsAny } from '../any/is_any.js'
import type { IsNever } from '../index.js'
import type { IsNever } from '../never/is_never.js'
import type { $Else, $SelectionBranch, $Then } from '../type_plus/branch/selection.js'

/**
* Check if `T` is a positive numeric type.
Expand All @@ -12,16 +13,13 @@ import type { IsNever } from '../index.js'
* type R = Positive<-1> // never
* ```
*/
export type Positive<T, Then = T, Else = never> = IsAny<
T,
{
$then: Then | Else,
$else: IsNever<T, {
$then: Else,
$else: T extends number | bigint ? (`${T}` extends `-${string}` ? Else : Then) : Else
}>
}
>
export type Positive<T, Then = T, Else = never> = IsAny<T, $SelectionBranch> extends infer R
? R extends $Then ? Then | Else
: R extends $Else ? (IsNever<T, $SelectionBranch> extends infer R2
? R2 extends $Then ? Else
: R2 extends $Else ? T extends number | bigint ? (`${T}` extends `-${string}` ? Else : Then) : Else
: never : never)
: never : never

/**
* Is `T` a positive numeric type.
Expand Down
10 changes: 5 additions & 5 deletions type-plus/ts/string/strict_string_type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IsAny } from '../any/is_any.js'
import type { $Else, $SelectionBranch, $Then } from '../type_plus/branch/selection.js'

/**
* Check if the type `T` is exactly `string`.
Expand All @@ -15,11 +16,10 @@ import type { IsAny } from '../any/is_any.js'
*/
export type StrictStringType<T, Then = T, Else = never> = IsAny<
T,
{
$then: Else,
$else: [T, string] extends [string, T] ? Then : Else
}
>
$SelectionBranch> extends infer R
? R extends $Then ? Else
: R extends $Else ? [T, string] extends [string, T] ? Then : Else
: never : never

/**
* Is the type `T` exactly `string`.
Expand Down
12 changes: 6 additions & 6 deletions type-plus/ts/unknown/unknown_type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IsAny } from '../any/is_any.js'
import type { $SelectionFilter, $SelectionOptions } from '../type_plus/branch/selection.js'
import type { $Else, $SelectionBranch, $SelectionFilter, $SelectionOptions, $Then } from '../type_plus/branch/selection.js'

/**
* Check if the type `T` is exactly `unknown`.
Expand All @@ -16,8 +16,8 @@ export type UnknownType<
$Options extends $SelectionOptions = $SelectionFilter<T>
> = IsAny<
T,
{
$then: $Options['$else'],
$else: [T, unknown] extends [unknown, T] ? $Options['$then'] : $Options['$else']
}
>
$SelectionBranch> extends infer R
? R extends $Then ? $Options['$else']
: R extends $Else ? [T, unknown] extends [unknown, T] ? $Options['$then'] : $Options['$else']
: never : never

0 comments on commit 6a0481d

Please sign in to comment.