Skip to content

Commit

Permalink
rename infer/validate under type namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad committed Sep 3, 2024
1 parent 9adf385 commit 86d4751
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 164 deletions.
4 changes: 2 additions & 2 deletions ark/attest/assert/chainableAssertions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { caller } from "@ark/fs"
import { printable, snapshot, type Constructor } from "@ark/util"
import prettier from "@prettier/sync"
import { type, type validateTypeRoot } from "arktype"
import { type } from "arktype"
import * as assert from "node:assert/strict"
import { isDeepStrictEqual } from "node:util"
import {
Expand Down Expand Up @@ -307,7 +307,7 @@ export type comparableValueAssertion<expected, kind extends AssertionKind> = {
instanceOf: (constructor: Constructor) => nextAssertions<kind>
is: (value: expected) => nextAssertions<kind>
completions: (value?: Completions) => void
satisfies: <def>(def: validateTypeRoot<def>) => nextAssertions<kind>
satisfies: <def>(def: type.validate<def>) => nextAssertions<kind>
// This can be used to assert values without type constraints
unknown: Omit<comparableValueAssertion<unknown, kind>, "unknown">
}
Expand Down
86 changes: 43 additions & 43 deletions ark/repo/scratch/fn.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
import type { inferTypeRoot, validateTypeRoot } from "arktype"
import type { type } from "arktype"
import type { ArkAmbient } from "arktype/config"

export type FunctionParser<$> = {
// <ret = unknown>(_?: ":", ret?: validateTypeRoot<ret, $>): <
// <ret = unknown>(_?: ":", ret?: type.validate<ret, $>): <
// implementation extends () => unknown extends ret
// ? unknown
// : inferTypeRoot<ret, $>
// : type.infer<ret, $>
// >(
// implementation: implementation
// ) => implementation

<arg0, ret = unknown>(
arg0: validateTypeRoot<arg0, $>,
arg0: type.validate<arg0, $>,
_?: ":",
ret?: validateTypeRoot<ret, $>
ret?: type.validate<ret, $>
): <
implementation extends (
arg0: inferTypeRoot<arg0, $>
) => unknown extends ret ? unknown : inferTypeRoot<ret, $>
arg0: type.infer<arg0, $>
) => unknown extends ret ? unknown : type.infer<ret, $>
>(
implementation: implementation
) => implementation

<arg0, arg1, ret = unknown>(
arg0: validateTypeRoot<arg0, $>,
arg1: validateTypeRoot<arg1, $>,
arg0: type.validate<arg0, $>,
arg1: type.validate<arg1, $>,
_?: ":",
ret?: validateTypeRoot<ret, $>
ret?: type.validate<ret, $>
): <
implementation extends (
arg0: inferTypeRoot<arg0, $>,
arg1: inferTypeRoot<arg1, $>
) => unknown extends ret ? unknown : inferTypeRoot<ret, $>
arg0: type.infer<arg0, $>,
arg1: type.infer<arg1, $>
) => unknown extends ret ? unknown : type.infer<ret, $>
>(
implementation: implementation
) => implementation

<arg0, arg1, arg2, ret = unknown>(
arg0: validateTypeRoot<arg0, $>,
arg1: validateTypeRoot<arg1, $>,
arg2: validateTypeRoot<arg2, $>,
arg0: type.validate<arg0, $>,
arg1: type.validate<arg1, $>,
arg2: type.validate<arg2, $>,
_?: ":",
ret?: validateTypeRoot<ret, $>
ret?: type.validate<ret, $>
): <
implementation extends (
arg0: inferTypeRoot<arg0, $>,
arg1: inferTypeRoot<arg1, $>,
arg2: inferTypeRoot<arg2, $>
) => unknown extends ret ? unknown : inferTypeRoot<ret, $>
arg0: type.infer<arg0, $>,
arg1: type.infer<arg1, $>,
arg2: type.infer<arg2, $>
) => unknown extends ret ? unknown : type.infer<ret, $>
>(
implementation: implementation
) => implementation

<arg0, arg1, arg2, arg3, ret = unknown>(
arg0: validateTypeRoot<arg0, $>,
arg1: validateTypeRoot<arg1, $>,
arg2: validateTypeRoot<arg2, $>,
arg3: validateTypeRoot<arg3, $>,
arg0: type.validate<arg0, $>,
arg1: type.validate<arg1, $>,
arg2: type.validate<arg2, $>,
arg3: type.validate<arg3, $>,
_?: ":",
ret?: validateTypeRoot<ret, $>
ret?: type.validate<ret, $>
): <
implementation extends (
arg0: inferTypeRoot<arg0, $>,
arg1: inferTypeRoot<arg1, $>,
arg2: inferTypeRoot<arg2, $>,
arg3: inferTypeRoot<arg3, $>
) => unknown extends ret ? unknown : inferTypeRoot<ret, $>
arg0: type.infer<arg0, $>,
arg1: type.infer<arg1, $>,
arg2: type.infer<arg2, $>,
arg3: type.infer<arg3, $>
) => unknown extends ret ? unknown : type.infer<ret, $>
>(
implementation: implementation
) => implementation

<arg0, arg1, arg2, arg3, arg4, ret = unknown>(
arg0: validateTypeRoot<arg0, $>,
arg1: validateTypeRoot<arg1, $>,
arg2: validateTypeRoot<arg2, $>,
arg3: validateTypeRoot<arg3, $>,
arg4: validateTypeRoot<arg4, $>,
arg0: type.validate<arg0, $>,
arg1: type.validate<arg1, $>,
arg2: type.validate<arg2, $>,
arg3: type.validate<arg3, $>,
arg4: type.validate<arg4, $>,
_?: ":",
ret?: validateTypeRoot<ret, $>
ret?: type.validate<ret, $>
): <
implementation extends (
arg0: inferTypeRoot<arg0, $>,
arg1: inferTypeRoot<arg1, $>,
arg2: inferTypeRoot<arg2, $>,
arg3: inferTypeRoot<arg3, $>,
arg4: inferTypeRoot<arg4, $>
) => unknown extends ret ? unknown : inferTypeRoot<ret, $>
arg0: type.infer<arg0, $>,
arg1: type.infer<arg1, $>,
arg2: type.infer<arg2, $>,
arg3: type.infer<arg3, $>,
arg4: type.infer<arg4, $>
) => unknown extends ret ? unknown : type.infer<ret, $>
>(
implementation: implementation
) => implementation
Expand Down
10 changes: 5 additions & 5 deletions ark/repo/scratch/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// import type { distill, Narrowed } from "./ast.ts"
// import type { inferIntersection } from "./intersect.ts"
// import type { Scope } from "./scope.ts"
// import type { inferTypeRoot, validateTypeRoot } from "./type.ts"
// import type { type.infer, type.validate } from "./type.ts"

// type MatchParserContext = {
// thens: readonly ((In: unknown) => unknown)[]
Expand Down Expand Up @@ -56,16 +56,16 @@
// > = override<ctx, { thens: [...ctx["thens"], ...branches] }>

// type validateWhenDefinition<def, ctx extends MatchParserContext> =
// def extends validateTypeRoot<def, ctx["$"]> ?
// def extends type.validate<def, ctx["$"]> ?
// inferMatchBranch<def, ctx> extends getHandledBranches<ctx> ?
// ErrorMessage<"This branch is redundant and will never be reached">
// : def
// : validateTypeRoot<def, ctx["$"]>
// : type.validate<def, ctx["$"]>

// // infer the types handled by a match branch, which is identical to `inferTypeRoot` while properly
// // infer the types handled by a match branch, which is identical to `type.infer` while properly
// // excluding cases that are already handled by other branches
// type inferMatchBranch<def, ctx extends MatchParserContext> = distill.Out<
// inferIntersection<getUnhandledBranches<ctx>, inferTypeRoot<def, ctx["$"]>>
// inferIntersection<getUnhandledBranches<ctx>, type.infer<def, ctx["$"]>>
// >

// export type ChainableMatchParser<ctx extends MatchParserContext> = {
Expand Down
22 changes: 8 additions & 14 deletions ark/repo/scratch/typeClass.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { DynamicBase } from "@ark/util"
import {
type,
type inferAmbient,
type inferTypeRoot,
type validateAmbient,
type validateTypeRoot
} from "arktype"

const Class = <def>(def: validateAmbient<def>) => {
import { type } from "arktype"

const Class = <def>(def: type.validate<def>) => {
const validator = type(def as never)

return class TypeConstructor<t = inferAmbient<def>> extends DynamicBase<
return class TypeConstructor<t = type.infer<def>> extends DynamicBase<
t & object
> {
static infer: inferAmbient<def>
static infer: type.infer<def>

constructor(input: unknown) {
const out = validator(input)
Expand All @@ -25,12 +19,12 @@ const Class = <def>(def: validateAmbient<def>) => {

static and<cls extends typeof TypeConstructor, andDef>(
this: cls,
def: validateAmbient<andDef>
def: type.validate<andDef>
) {
return class extends (this as typeof TypeConstructor<
InstanceType<cls> & inferAmbient<andDef>
InstanceType<cls> & type.infer<andDef>
>) {
static infer: cls["infer"] & inferAmbient<andDef>
static infer: cls["infer"] & type.infer<andDef>
}
}
}
Expand Down
70 changes: 35 additions & 35 deletions ark/type/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
type Json,
type WhiteSpaceToken
} from "@ark/util"
import type { type } from "./keywords/ark.ts"
import type {
inferDefinition,
validateDefinition
Expand All @@ -29,7 +30,7 @@ import type { state, StaticState } from "./parser/string/reduce/static.ts"
import { Scanner } from "./parser/string/shift/scanner.ts"
import { parseUntilFinalizer } from "./parser/string/string.ts"
import type { ParseContext, Scope } from "./scope.ts"
import type { inferTypeRoot, Type, validateTypeRoot } from "./type.ts"
import type { Type } from "./type.ts"

export type ParameterString<params extends string = string> = `<${params}>`

Expand All @@ -42,7 +43,7 @@ export type validateParameterString<s extends ParameterString, $> =
: s

export type validateGenericArg<arg, param extends GenericParamAst, $> =
inferTypeRoot<arg, $> extends param[1] ? unknown
type.infer<arg, $> extends param[1] ? unknown
: ErrorType<`Invalid argument for ${param[0]}`, [expected: param[1]]>

export type GenericInstantiator<
Expand All @@ -54,13 +55,13 @@ export type GenericInstantiator<
params["length"] extends 1 ?
{
<const a, r = instantiateGeneric<def, params, [a], $, args$>>(
a: validateTypeRoot<a, args$> & validateGenericArg<a, params[0], args$>
a: type.validate<a, args$> & validateGenericArg<a, params[0], args$>
): r
}
: params["length"] extends 2 ?
{
<const a, const b, r = instantiateGeneric<def, params, [a, b], $, args$>>(
...args: [validateTypeRoot<a, args$>, validateTypeRoot<b, args$>] &
...args: [type.validate<a, args$>, type.validate<b, args$>] &
[
validateGenericArg<a, params[0], args$>,
validateGenericArg<b, params[1], args$>
Expand All @@ -76,9 +77,9 @@ export type GenericInstantiator<
r = instantiateGeneric<def, params, [a, b, c], $, args$>
>(
...args: [
validateTypeRoot<a, args$>,
validateTypeRoot<b, args$>,
validateTypeRoot<c, args$>
type.validate<a, args$>,
type.validate<b, args$>,
type.validate<c, args$>
] &
[
validateGenericArg<a, params[0], args$>,
Expand All @@ -97,10 +98,10 @@ export type GenericInstantiator<
r = instantiateGeneric<def, params, [a, b, c, d], $, args$>
>(
...args: [
validateTypeRoot<a, args$>,
validateTypeRoot<b, args$>,
validateTypeRoot<c, args$>,
validateTypeRoot<d, args$>
type.validate<a, args$>,
type.validate<b, args$>,
type.validate<c, args$>,
type.validate<d, args$>
] &
[
validateGenericArg<a, params[0], args$>,
Expand All @@ -121,11 +122,11 @@ export type GenericInstantiator<
r = instantiateGeneric<def, params, [a, b, c, d, e], $, args$>
>(
...args: [
validateTypeRoot<a, args$>,
validateTypeRoot<b, args$>,
validateTypeRoot<c, args$>,
validateTypeRoot<d, args$>,
validateTypeRoot<e, args$>
type.validate<a, args$>,
type.validate<b, args$>,
type.validate<c, args$>,
type.validate<d, args$>,
type.validate<e, args$>
] &
[
validateGenericArg<a, params[0], args$>,
Expand All @@ -148,12 +149,12 @@ export type GenericInstantiator<
r = instantiateGeneric<def, params, [a, b, c, d, e, f], $, args$>
>(
...args: [
validateTypeRoot<a, args$>,
validateTypeRoot<b, args$>,
validateTypeRoot<c, args$>,
validateTypeRoot<d, args$>,
validateTypeRoot<e, args$>,
validateTypeRoot<f, args$>
type.validate<a, args$>,
type.validate<b, args$>,
type.validate<c, args$>,
type.validate<d, args$>,
type.validate<e, args$>,
type.validate<f, args$>
] &
[
validateGenericArg<a, params[0], args$>,
Expand All @@ -178,13 +179,13 @@ export type GenericInstantiator<
r = instantiateGeneric<def, params, [a, b, c, d, e, f, g], $, args$>
>(
...args: [
validateTypeRoot<a, args$>,
validateTypeRoot<b, args$>,
validateTypeRoot<c, args$>,
validateTypeRoot<d, args$>,
validateTypeRoot<e, args$>,
validateTypeRoot<f, args$>,
validateTypeRoot<g, args$>
type.validate<a, args$>,
type.validate<b, args$>,
type.validate<c, args$>,
type.validate<d, args$>,
type.validate<e, args$>,
type.validate<f, args$>,
type.validate<g, args$>
] &
[
validateGenericArg<a, params[0], args$>,
Expand All @@ -209,13 +210,13 @@ type instantiateGeneric<
args$
> = Type<
[def] extends [Hkt] ?
Hkt.apply<def, { [i in keyof args]: inferTypeRoot<args[i], args$> }>
Hkt.apply<def, { [i in keyof args]: type.infer<args[i], args$> }>
: inferDefinition<def, $, bindGenericArgs<params, args$, args>>,
args$
>

type bindGenericArgs<params extends array<GenericParamAst>, $, args> = {
[i in keyof params & `${number}` as params[i][0]]: inferTypeRoot<
[i in keyof params & `${number}` as params[i][0]]: type.infer<
args[i & keyof args],
$
>
Expand Down Expand Up @@ -391,9 +392,8 @@ type _parseOptionalConstraint<

type genericParamDefToAst<schema extends GenericParamDef, $> =
schema extends string ? [schema, unknown]
: schema extends readonly [infer name, infer def] ?
[name, inferTypeRoot<def, $>]
: never
: schema extends readonly [infer name, infer def] ? [name, type.infer<def, $>]
: never

export type genericParamDefsToAst<defs extends array<GenericParamDef>, $> = [
...{ [i in keyof defs]: genericParamDefToAst<defs[i], $> }
Expand All @@ -406,7 +406,7 @@ export type GenericParser<$ = {}> = <
[i in keyof paramsDef]: paramsDef[i] extends (
readonly [infer name, infer def]
) ?
readonly [name, validateTypeRoot<def, $>]
readonly [name, type.validate<def, $>]
: paramsDef[i]
}
) => GenericBodyParser<genericParamDefsToAst<paramsDef, $>, $>
Expand Down
8 changes: 1 addition & 7 deletions ark/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,4 @@ export {
type inferScope,
type validateScope
} from "./scope.ts"
export {
Type,
type inferAmbient,
type inferTypeRoot,
type validateAmbient,
type validateTypeRoot
} from "./type.ts"
export { Type } from "./type.ts"
Loading

0 comments on commit 86d4751

Please sign in to comment.