Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/website/public/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions apps/website/src/assets/type-plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
},
"files": {
"includes": [
"**",
"!**/.changeset/**/*.json",
"!**/.turbo",
"!**/.vscode/**/*.txt",
Expand Down
7 changes: 2 additions & 5 deletions packages/type-plus/src/$type/branch/$resolve-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ export type $ResolveBranch<
? _<$O, B, $ResolveBranch<$O, Rest, D>>
: $InferError<'$Branches must have at least one entry'>

type _<$O extends Record<string, any>, $B, D> = $B extends $Branch<any>
? $B[$Branch.$Key] extends keyof $O
? $O[$B[$Branch.$Key]]
: D
: D
type _<$O extends Record<string, any>, $B, D> =
$B extends $Branch<any> ? ($B[$Branch.$Key] extends keyof $O ? $O[$B[$Branch.$Key]] : D) : D
type _Last<$O extends Record<string, any>, $B, D> = $B extends $Then
? '$then' extends keyof $O
? $O['$then']
Expand Down
17 changes: 8 additions & 9 deletions packages/type-plus/src/$type/distributive/$distributive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ export namespace $Distributive {
/**
* Parse the options for `distributive`.
*/
export type Parse<$Options extends Options, $O extends $InputOptions<$Then | $Else> = {}> = $ResolveOptions<
[$Options['distributive'], Default['distributive']]
> extends true
? '$then' extends keyof $O
? $O['$then']
: true
: '$else' extends keyof $O
? $O['$else']
: false
export type Parse<$Options extends Options, $O extends $InputOptions<$Then | $Else> = {}> =
$ResolveOptions<[$Options['distributive'], Default['distributive']]> extends true
? '$then' extends keyof $O
? $O['$then']
: true
: '$else' extends keyof $O
? $O['$else']
: false
}
17 changes: 8 additions & 9 deletions packages/type-plus/src/$type/exact/$exact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ export namespace $Exact {
/**
* Parse the options for `exact`.
*/
export type Parse<$Options extends Options, $O extends $InputOptions<$Then | $Else> = {}> = $ResolveOptions<
[$Options['exact'], Default['exact']]
> extends true
? '$then' extends keyof $O
? $O['$then']
: true
: '$else' extends keyof $O
? $O['$else']
: false
export type Parse<$Options extends Options, $O extends $InputOptions<$Then | $Else> = {}> =
$ResolveOptions<[$Options['exact'], Default['exact']]> extends true
? '$then' extends keyof $O
? $O['$then']
: true
: '$else' extends keyof $O
? $O['$else']
: false
}
7 changes: 2 additions & 5 deletions packages/type-plus/src/$type/fn/_fn-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ import type { $Fn } from './$fn.js'
* `[R, true] extends [true, R]` holds only when `R` is `true` (or `any`):
* `boolean` fails the first position and `never` fails the second.
*/
export type _FnTest<T, F extends $Fn> = $Fn.Apply<F, T> extends infer R
? [R, true] extends [true, R]
? true
: false
: never
export type _FnTest<T, F extends $Fn> =
$Fn.Apply<F, T> extends infer R ? ([R, true] extends [true, R] ? true : false) : never
13 changes: 4 additions & 9 deletions packages/type-plus/src/$type/utils/$strict-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ describe('generic wrappers', () => {
})

it('can narrow with a conditional when it has no options of its own', () => {
type Narrow<T, $O extends $Selection.Options = {}> = $O extends $StrictOptions<$O, IsObject.$Options>
? IsObject<T, $O>
: never
type Narrow<T, $O extends $Selection.Options = {}> =
$O extends $StrictOptions<$O, IsObject.$Options> ? IsObject<T, $O> : never

testType.equal<Narrow<{}>, true>(true)
testType.equal<Narrow<{ a: 1 }, { selection: 'filter' }>, { a: 1 }>(true)
Expand Down Expand Up @@ -150,12 +149,8 @@ describe('$ForwardOptions', () => {
})

it('is needed: narrowing without removing the wrapper option answers never', () => {
type NoStrip<T, $O extends $StrictOptions<$O, IsNonEmptyObject$Options> = {}> = $O extends $StrictOptions<
$O,
IsObject.$Options
>
? IsObject<T, $O>
: never
type NoStrip<T, $O extends $StrictOptions<$O, IsNonEmptyObject$Options> = {}> =
$O extends $StrictOptions<$O, IsObject.$Options> ? IsObject<T, $O> : never

testType.equal<NoStrip<{ a: 1 }>, true>(true)
testType.equal<NoStrip<{ a: 1 }, { nonEmpty: true }>, never>(true)
Expand Down
7 changes: 2 additions & 5 deletions packages/type-plus/src/array/array-plus.drop-match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,5 @@ type _DropMatch<A extends Readonly<Array<unknown>>, Criteria> = A[0] extends Cri
? A
: Array<Exclude<A[0], Criteria>>

type _Fn<A extends Readonly<Array<unknown>>, F extends $Fn> = _Exclude<A[number], F> extends infer R
? [R] extends [never]
? never[]
: Array<R>
: never
type _Fn<A extends Readonly<Array<unknown>>, F extends $Fn> =
_Exclude<A[number], F> extends infer R ? ([R] extends [never] ? never[] : Array<R>) : never
17 changes: 9 additions & 8 deletions packages/type-plus/src/array/array-plus.element-match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ type _ElementMatch<T, Criteria, $O extends ElementMatch.$Options> = [T] extends
: C['$notMatch']
: never

type _Fn<T, F extends $Fn, $O extends ElementMatch.$Options> = TypePlusOptions.Merge<
$O,
ElementMatch.$Default<F>
> extends infer C extends Record<keyof ElementMatch.$Options, unknown>
? (T extends unknown ? (_FnTest<T, F> extends true ? T : C['$notMatch']) : never) extends infer R
? IsUnion<T, { $then: IsNever<R, { $then: R; $else: R | C['$unionNotMatch'] }>; $else: R }>
: C['$notMatch']
: never
type _Fn<T, F extends $Fn, $O extends ElementMatch.$Options> =
TypePlusOptions.Merge<$O, ElementMatch.$Default<F>> extends infer C extends Record<
keyof ElementMatch.$Options,
unknown
>
? (T extends unknown ? (_FnTest<T, F> extends true ? T : C['$notMatch']) : never) extends infer R
? IsUnion<T, { $then: IsNever<R, { $then: R; $else: R | C['$unionNotMatch'] }>; $else: R }>
: C['$notMatch']
: never
42 changes: 18 additions & 24 deletions packages/type-plus/src/array/array-plus.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,22 @@ import type { TypePlusOptions } from '../utils/options.js'
* type R = ArrayPlus.Filter<Array<1 | 2>, IsObject.$Fn> // never[]
* ```
*/
export type Filter<
A extends readonly unknown[],
Criteria = true,
$O extends $StrictOptions<$O, Filter.$Options> = {},
> = TypePlusOptions.Merge<$O, Filter.$Default> extends infer O extends Filter.$Options
? IsNever<
A,
{
$then: O['$never']
$else: [Criteria] extends [never]
? _Type<A, Criteria, O>
: [Criteria] extends [infer F extends $Fn]
? number extends A['length']
? _Fn<A, F>
: O['$notArray']
: _Type<A, Criteria, O>
}
>
: never
export type Filter<A extends readonly unknown[], Criteria = true, $O extends $StrictOptions<$O, Filter.$Options> = {}> =
TypePlusOptions.Merge<$O, Filter.$Default> extends infer O extends Filter.$Options
? IsNever<
A,
{
$then: O['$never']
$else: [Criteria] extends [never]
? _Type<A, Criteria, O>
: [Criteria] extends [infer F extends $Fn]
? number extends A['length']
? _Fn<A, F>
: O['$notArray']
: _Type<A, Criteria, O>
}
>
: never

export namespace Filter {
export interface $Options extends $Never.$Options {
Expand All @@ -57,11 +54,8 @@ type _Type<A extends readonly unknown[], Criteria, O extends Filter.$Options> =
? Array<Criteria>
: O['$notArray']

type _Fn<A extends readonly unknown[], F extends $Fn> = _Keep<A[number], F> extends infer R
? [R] extends [never]
? never[]
: Array<R>
: never
type _Fn<A extends readonly unknown[], F extends $Fn> =
_Keep<A[number], F> extends infer R ? ([R] extends [never] ? never[] : Array<R>) : never

/**
* The members of `T` that the type function `F` matches.
Expand Down
59 changes: 28 additions & 31 deletions packages/type-plus/src/array/at.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,34 @@ import type { IndexAt } from './array-plus.index-at.js'
* type R = At<string[], 0> // string | undefined
* ```
*/
export type At<
A extends readonly unknown[],
N extends number,
$O extends $StrictOptions<$O, At.$Options> = {},
> = IndexAt<
A,
N,
{
$never: _ResolveFail<$O>
$emptyTuple: _ResolveFail<$O>
$upperBound: _ResolveFail<$O>
$lowerBound: _ResolveFail<$O>
}
> extends infer I
? I extends number
? IsTuple.$<
A,
{
$then: IsNumber.$<
I,
{
exact: true
$then: A[I] | undefined
$else: A[I]
}
>
$else: A[I] | undefined
}
>
: _ResolveFail<$O>
: never
export type At<A extends readonly unknown[], N extends number, $O extends $StrictOptions<$O, At.$Options> = {}> =
IndexAt<
A,
N,
{
$never: _ResolveFail<$O>
$emptyTuple: _ResolveFail<$O>
$upperBound: _ResolveFail<$O>
$lowerBound: _ResolveFail<$O>
}
> extends infer I
? I extends number
? IsTuple.$<
A,
{
$then: IsNumber.$<
I,
{
exact: true
$then: A[I] | undefined
$else: A[I]
}
>
$else: A[I] | undefined
}
>
: _ResolveFail<$O>
: never

export namespace At {
export interface $Options extends $Fail.$Options {}
Expand Down
22 changes: 10 additions & 12 deletions packages/type-plus/src/array/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@ import type { TypePlusOptions } from '../utils/options.js'
* @typeParam $O['$emptyTuple'] Return type when `A` is `[]`.
* Default to `never`.
*/
export type Head<A extends readonly unknown[], $O extends $StrictOptions<$O, Head.$Options> = {}> = IsNever<
A,
$Selection.Branch
> extends infer R
? R extends $Then
? TypePlusOptions.Merge<$O, Head.$Default>['$never']
: R extends $Else
? A['length'] extends 0
? TypePlusOptions.Merge<$O, Head.$Default>['$emptyTuple']
: A[0]
: never
: never
export type Head<A extends readonly unknown[], $O extends $StrictOptions<$O, Head.$Options> = {}> =
IsNever<A, $Selection.Branch> extends infer R
? R extends $Then
? TypePlusOptions.Merge<$O, Head.$Default>['$never']
: R extends $Else
? A['length'] extends 0
? TypePlusOptions.Merge<$O, Head.$Default>['$emptyTuple']
: A[0]
: never
: never

export namespace Head {
export interface $Options extends $Never.$Options {
Expand Down
22 changes: 10 additions & 12 deletions packages/type-plus/src/array/last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ import type { TypePlusOptions } from '../utils/options.js'
* @typeParam $O['$emptyTuple'] Return type when `A` is `[]`.
* Default to `never`.
*/
export type Last<
A extends readonly unknown[],
$O extends $StrictOptions<$O, Last.$Options> = {},
> = TypePlusOptions.Merge<$O, Last.$Default> extends infer O extends Required<Last.$Options>
? IsNever<
A,
{
$then: O['$never']
$else: A['length'] extends 0 ? O['$emptyTuple'] : A extends readonly [...unknown[], infer R] ? R : A[0]
}
>
: never
export type Last<A extends readonly unknown[], $O extends $StrictOptions<$O, Last.$Options> = {}> =
TypePlusOptions.Merge<$O, Last.$Default> extends infer O extends Required<Last.$Options>
? IsNever<
A,
{
$then: O['$never']
$else: A['length'] extends 0 ? O['$emptyTuple'] : A extends readonly [...unknown[], infer R] ? R : A[0]
}
>
: never

export namespace Last {
export interface $Options extends $Never.$Options {
Expand Down
8 changes: 2 additions & 6 deletions packages/type-plus/src/array/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ type _IndexKind<N extends number> = IsAny<
}
>

type _Slice<A extends readonly unknown[], Start extends number, End extends number> = GreaterThan<
End,
Start
> extends true
? _Take<A, Start, End>
: []
type _Slice<A extends readonly unknown[], Start extends number, End extends number> =
GreaterThan<End, Start> extends true ? _Take<A, Start, End> : []

type _Take<
A extends readonly unknown[],
Expand Down
7 changes: 4 additions & 3 deletions packages/type-plus/src/bigint/is-bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ export namespace IsBigint {
* This is a type util for building custom types.
* It does not check against special types.
*/
export type $<T, $O extends $UtilOptions> = $ResolveOptions<[$O['exact'], $Exact.Default]> extends true
? $Distributive.Parse<$O, { $then: _SD<T, $O>; $else: _SN<T, $O> }>
: Assignable.$<T, bigint, $O>
export type $<T, $O extends $UtilOptions> =
$ResolveOptions<[$O['exact'], $Exact.Default]> extends true
? $Distributive.Parse<$O, { $then: _SD<T, $O>; $else: _SN<T, $O> }>
: Assignable.$<T, bigint, $O>
}

type $UtilOptions = $Selection.Options & $Distributive.Options & $Exact.Options
Expand Down
7 changes: 4 additions & 3 deletions packages/type-plus/src/bigint/is-not-bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ export namespace IsNotBigint {
* This is a type util for building custom types.
* It does not check against special types.
*/
export type $<T, $O extends $UtilOptions> = $ResolveOptions<[$O['exact'], false]> extends true
? $Distributive.Parse<$O, { $then: _SD<T, $O>; $else: _SN<T, $O> }>
: NotAssignable.$<T, bigint, $O>
export type $<T, $O extends $UtilOptions> =
$ResolveOptions<[$O['exact'], false]> extends true
? $Distributive.Parse<$O, { $then: _SD<T, $O>; $else: _SN<T, $O> }>
: NotAssignable.$<T, bigint, $O>
}

type $UtilOptions = $Selection.Options & $Distributive.Options & $Exact.Options
Expand Down
Loading
Loading