Skip to content

Commit

Permalink
chore: move around aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrjarrett committed May 8, 2024
1 parent 1275399 commit b7fa27b
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 156 deletions.
48 changes: 28 additions & 20 deletions src/check/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,27 +226,11 @@ type isTuple<type, constraint extends any.array = any.array, hush = never.unused
: ([hush] extends [never.unused_arg] ? typeError<`Expected a tuple`, [type]> : never.prevent_match)
;

declare namespace is {
export {
isStringLiteral as stringLiteral,
isNumberLiteral as numberLiteral,
isBooleanLiteral as booleanLiteral,
isLiteral as literal,
isUnion as union,
isTuple as tuple,
}
}
type isNonEmptyObject<type, hush = never>
= [keyof type] extends [never] ? [hush] extends [never] ? typeError<`Expected a non-empty object`, [{}]> : never : {}

declare namespace non {
export {
isNonArrayObject as array,
isNonUnion as union,
isNonLiteral as literal,
isNonFiniteBoolean as finiteBoolean,
isNonFiniteString as finiteString,
isNonFiniteNumber as finiteNumber,
}
}
type isEmptyObject<type, hush = never>
= [keyof type] extends [never] ? {} : [hush] extends [never] ? typeError<`Expected an empty object`, [{}]> : never

type check<type, invariant> = [type] extends [invariant] ? invariant : doesNotSatisfy<invariant, type>

Expand All @@ -264,6 +248,30 @@ declare namespace check {
violatesRuleWithMsg,
}

namespace is {
export {
isBooleanLiteral as booleanLiteral,
isEmptyObject as emptyObject,
isNumberLiteral as numberLiteral,
isStringLiteral as stringLiteral,
isLiteral as literal,
isUnion as union,
isTuple as tuple,
}
}

namespace non {
export {
isNonArrayObject as array,
isNonUnion as union,
isNonEmptyObject as emptyObject,
isNonLiteral as literal,
isNonFiniteBoolean as finiteBoolean,
isNonFiniteString as finiteString,
isNonFiniteNumber as finiteNumber,
}
}

export namespace strict {
/**
* @example
Expand Down
6 changes: 3 additions & 3 deletions src/empty.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { any } from "./any/exports.js"
import type { _ } from "./util.js"

export {
type empty,
type nonempty,
export type {
empty,
nonempty,
}

declare namespace empty {
Expand Down
4 changes: 2 additions & 2 deletions src/err/enforce.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type {
enforce,
constrain,
Partial,
partial as Partial,
}

/** @internal - only exported for testing purposes */
Expand All @@ -17,7 +17,7 @@ import type { HasDiscriminant } from "../tag/tag.js"
import type { Union as U } from "../union/exports.js"
import type { empty } from "../empty.js"

declare namespace Partial {
declare namespace partial {
type strict<type extends globalThis.Partial<invariant>, invariant> = (
& globalThis.Partial<invariant>
& { [ix in Exclude<keyof type, keyof invariant>]?: never }
Expand Down
44 changes: 22 additions & 22 deletions src/err/err.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

export {
type TypeError,
type typeError as TypeError,
type Err,
type Err2,
type URI,
Expand Down Expand Up @@ -78,31 +78,31 @@ const Msg2 = {
*
* TODO: make this feature configurable globally.
*/
type typeError<msg extends string, error, debugFriendly = never>
type typeErrorReturn<msg extends string, error, debugFriendly = never>
= never | (
[debugFriendly] extends [never]
? TypeError<[msg: msg, got: error]>
: TypeError<[𝗺𝘀𝗴: msg, 𝗴𝗼𝘁: error]>
? typeError<[msg: msg, got: error]>
: typeError<[𝗺𝘀𝗴: msg, 𝗴𝗼𝘁: error]>
)
;

/** @category model */
interface TypeError<map extends readonly [msg: string, error: unknown]>
extends TypeError.new<[map[0], map[1]]> { [URI.TypeError]: URI.TypeError }
interface typeError<map extends readonly [msg: string, error: unknown]>
extends typeError.new<[map[0], map[1]]> { [URI.TypeError]: URI.TypeError }

/** @category coproduct, structure-preserving */
type Err = typeof Err
const Err
: { [tag in keyof Msg]: TypeError.template<[discriminant: tag]> }
: { [tag in keyof Msg]: typeError.template<[discriminant: tag]> }
= Object.keys(Msg).reduce(
(acc: any.struct, tag) => ({ ...acc, [tag]: TypeError.template(tag) }),
(acc: any.struct, tag) => ({ ...acc, [tag]: typeError.template(tag) }),
{}
) as never

type Err2<key extends keyof Msg2, type>
= ({ [tag in keyof Msg2]: TypeError.template2<[discriminant: tag], type> })[key]
= ({ [tag in keyof Msg2]: typeError.template2<[discriminant: tag], type> })[key]

declare namespace TypeError {
declare namespace typeError {
export {
/** @category function application */
catch_ as catch,
Expand All @@ -112,15 +112,15 @@ declare namespace TypeError {

/** @category type constructors */
type new_<map extends readonly [msg: string, got: unknown]>
= TypeError.catch<TypeError.bind<map[0]>, map[1]>
= typeError.catch<typeError.bind<map[0]>, map[1]>

/** @category type constructors */
export interface template<errorTag extends readonly [tag: keyof Msg]> {
<const type>(type: type): typeError<Msg[errorTag[0]], type>
// <const type extends any.two>(type: type): typeError<Msg[errorTag[0]], type>
<const type>(type: type): typeErrorReturn<Msg[errorTag[0]], type>
// <const type extends any.two>(type: type): typeErrorReturn<Msg[errorTag[0]], type>
}

export type template2<errorTag extends readonly [tag: keyof Msg2], type> = typeError<Msg2[errorTag[0]], type>
export type template2<errorTag extends readonly [tag: keyof Msg2], type> = typeErrorReturn<Msg2[errorTag[0]], type>

/** @category function application */
export interface bind<message extends string> {
Expand All @@ -130,12 +130,12 @@ declare namespace TypeError {
}

/** @category function application */
type catch_<template extends TypeError.bind<string>, error>
type catch_<template extends typeError.bind<string>, error>
= (template & { error: error })["capture"]
;
}

declare namespace TypeError {
declare namespace typeError {
interface Template {
[0]: unknown /// inbound
[-1]: unknown /// error channel
Expand All @@ -149,7 +149,7 @@ declare namespace TypeError {
type eval<type> = never | { [ix in keyof type]: type[ix] }
type _5 = eval<render<Template, 56>>

export type typecheck<type> = [type] extends [Template] ? TypeError.throw<type> : type
export type typecheck<type> = [type] extends [Template] ? typeError.throw<type> : type
export { typecheck as try }

namespace not {
Expand All @@ -176,7 +176,7 @@ declare namespace TypeError {
* // ^? const throwsTypeError: "Did not expect to receive a type assignable to `100`"
*/
function assignableTo<const disallow>(disallow: disallow)
: <const type extends not.assignableTo<disallow, type>>(type: type) => TypeError.try<type>
: <const type extends not.assignableTo<disallow, type>>(type: type) => typeError.try<type>
function assignableTo<
const disallow,
template extends Template
Expand All @@ -185,16 +185,16 @@ declare namespace TypeError {
template: template
)
: <const type extends not.assignableToWithMsg<disallow, type>>(type: type)
=> TypeError.try<type>
=> typeError.try<type>

}
}
}

namespace TypeError {
namespace typeError {
/** @category term constructors */
export function template<tag extends keyof Msg, meta>(tag: tag): <const err>(err: err) => typeError<Msg[tag], err>
export function template<tag extends any.string>(tag: tag): <const err>(type: err) => typeError<tag, err>
export function template<tag extends keyof Msg, meta>(tag: tag): <const err>(err: err) => typeErrorReturn<Msg[tag], err>
export function template<tag extends any.string>(tag: tag): <const err>(type: err) => typeErrorReturn<tag, err>
export function template<tag extends keyof Msg>(tag: tag): unknown {
return <const err>(err: err) => ({
error: err,
Expand Down
47 changes: 24 additions & 23 deletions src/kind/kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,28 @@ declare namespace kind {
bind$,
check,
constraintsOf,
partial,
partiallyApply,
// partial,
// partiallyApply,
satisfies,
}

/**
* ### {@link check `Kind.check`}
* ---
* Given a {@link Kind `Kind`} and a set of arguments, checks that the arguments
* satisfy the Kind's constraints.
*
* '{}' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'object'.
*
* Because {@link apply `Kind.apply`} (along with {@link bind `Kind.bind`},
* {@link call `Kind.call`}, etc.) requires the user
*
* introspect the `Kind`
*
*
*/
type check<fn extends Kind, args extends { [x: number]: unknown }> = args extends satisfies<fn> ? args : never

}

export interface Fn1 { [0]: _ }
Expand Down Expand Up @@ -140,23 +158,6 @@ type inferConstraints<fn extends Kind>
: never.close.inline_var<"constraints">
;

/**
* ### {@link check `Kind.check`}
* ---
* Given a {@link Kind `Kind`} and a set of arguments, checks that the arguments
* satisfy the Kind's constraints.
*
* '{}' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'object'.
*
* Because {@link apply `Kind.apply`} (along with {@link bind `Kind.bind`},
* {@link call `Kind.call`}, etc.) requires the user
*
* introspect the `Kind`
*
*
*/
type check<fn extends Kind, args extends { [x: number]: unknown }> = args extends satisfies<fn> ? args : never

type identity<type> = type
type nonunion<type> = [Union.is<type>] extends [true] ? ReturnType<typeof Err.NonUnion> : unknown

Expand Down Expand Up @@ -224,10 +225,10 @@ interface Mutable<type extends {}> extends identity<type> { }
//////////////////
/// DEPRECATED ///
//////////////////
/** @deprecated use {@link kind.bind `Kind.bind`} instead */
type partial<fn extends Kind, args extends Partial<satisfies<fn>>> = never | (fn & structured<args>)
/** @deprecated use {@link kind.apply `Kind.apply`} instead */
type partiallyApply<fn extends Kind, args extends Partial<satisfies<fn>>> = partial<fn, args>[-1]
// /** @deprecated use {@link kind.bind `Kind.bind`} instead */
// type partial<fn extends Kind, args extends globalThis.Partial<satisfies<fn>>> = never | (fn & structured<args>)
// /** @deprecated use {@link kind.apply `Kind.apply`} instead */
// type partiallyApply<fn extends Kind, args extends globalThis.Partial<satisfies<fn>>> = partial<fn, args>[-1]

/**
* {@link reduce `reduce`}
Expand Down
57 changes: 29 additions & 28 deletions src/number/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,11 @@ import type { Err } from "../exports.js"
import type { $$, newtype } from "./shared.js"


type enforce<x>
= number extends x ? Fn.return<typeof Err.Literal<x>>
/** bigints are integers */
: bigint extends x ? unknown
: [x] extends [any.showable]
? [typecheck<x>] extends [true] ? (number | bigint)
: Fn.return<typeof Err.Integer<x>>
: Fn.return<typeof Err.Numeric<x>>
;

type typecheck<type>
= [type] extends [string] ? false
: [type] extends [never] ? false
: [`${type & any.showable}`] extends [`${bigint}` | `${bigint}e+${bigint}`]
? true
: false
;

type narrow<x extends number> = [x] extends [enforce<x>] ? x : number extends x ? number & int<number> : never

type is<type> = [typecheck<type>] extends [true] ? true : false
declare function is<x extends number>(u: int<x>): u is int<x>
declare function is<x extends number>(x: x): x is narrow<x>
declare function is(x: number): x is number & int<number>
declare function is(u: unknown): u is int<number>

/** @internal */
interface int<x extends number> extends newtype.newtype<$$.int, x> { }

type Int<x extends enforce<x>> = never | (x extends number ? int<x> : x)
declare function Int<const x extends enforce<x>>(x: x): Int<x>
type Int<x extends Int.enforce<x>> = never | (x extends number ? int<x> : x)
declare function Int<const x extends Int.enforce<x>>(x: x): Int<x>
// example impl: function Int<const x extends number>(x: x) { return Number.isInteger(x) as never }

/**
Expand All @@ -51,4 +25,31 @@ declare namespace Int {
enforce,
typecheck,
}

type enforce<x>
= number extends x ? Fn.return<typeof Err.Literal<x>>
/** bigints are integers */
: bigint extends x ? unknown
: [x] extends [any.showable]
? [typecheck<x>] extends [true] ? (number | bigint)
: Fn.return<typeof Err.Integer<x>>
: Fn.return<typeof Err.Numeric<x>>
;

type typecheck<type>
= [type] extends [string] ? false
: [type] extends [never] ? false
: [`${type & any.showable}`] extends [`${bigint}` | `${bigint}e+${bigint}`]
? true
: false
;

type narrow<x extends number> = [x] extends [enforce<x>] ? x : number extends x ? number & int<number> : never

type is<type> = [typecheck<type>] extends [true] ? true : false
function is<x extends number>(u: int<x>): u is int<x>
function is<x extends number>(x: x): x is narrow<x>
function is(x: number): x is number & int<number>
function is(u: unknown): u is int<number>

}
4 changes: 2 additions & 2 deletions src/tree/tree.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Tree, nonempty as nonemptyPath } from "./tree.js"
import type { Tree } from "./tree.js"

import type { any } from "../any/exports.js"
import type { empty, nonempty } from "../empty.js"
Expand Down Expand Up @@ -26,7 +26,7 @@ type unfoldDepthFirst<path extends any.path, leaf>

type fromPaths<paths extends Tree.pathable | any.array<Tree.pathable>>
= (paths extends Tree.pathable ? paths : paths[number]) extends Tree.pathable<infer p>
? (p extends nonemptyPath<infer leaf, infer init> ? [leaf: leaf, init: init] : "BOB") extends infer out
? (p extends Tree.nonempty<infer leaf, infer init> ? [leaf: leaf, init: init] : "BOB") extends infer out
? [out] extends [[any, any.path]] ? depthFirst<out[0], out[1]>
: never
// unfoldDepthFirst<init, leaf>
Expand Down
Loading

0 comments on commit b7fa27b

Please sign in to comment.