Skip to content

Commit

Permalink
remove type as
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad committed Nov 26, 2024
1 parent 029c5fc commit 5dd8b6f
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 33 deletions.
14 changes: 7 additions & 7 deletions ark/type/__tests__/cast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,40 @@ import { type, type Type } from "arktype"
contextualize(() => {
describe("type.cast", () => {
it("primitive", () => {
const foo = type("string" as type.as<"foo">).t
const foo = type("string" as type.cast<"foo">).t
attest<"foo">(foo)
})

it("object", () => {
// definitions that are cast can't be validated
attest<{ a: "foo" }>(type({ a: "string" } as type.as<{ a: "foo" }>).t)
attest<{ a: "foo" }>(type({ a: "string" } as type.cast<{ a: "foo" }>).t)
})

it("primitive to object", () => {
attest<{ a: "foo" }>(type("string" as type.as<{ a: "foo" }>).t)
attest<{ a: "foo" }>(type("string" as type.cast<{ a: "foo" }>).t)
})

it("object to primitive", () => {
attest<"foo">(type({ a: "string" } as type.as<"foo">).t)
attest<"foo">(type({ a: "string" } as type.cast<"foo">).t)
})

it("infer function", () => {
type F = () => boolean
const constructable = type({} as type.as<F>)
const constructable = type({} as type.cast<F>)
attest<F>(constructable.t)
attest<F>(constructable.infer)
attest<F>(constructable.in.infer)
})

it("infer constructable", () => {
const constructable = type({} as type.as<Constructor>)
const constructable = type({} as type.cast<Constructor>)
attest<Constructor>(constructable.t)
attest<Constructor>(constructable.infer)
attest<Constructor>(constructable.in.infer)
})

it("undefined", () => {
const foo = type("string" as type.as<"foo">).t
const foo = type("string" as type.cast<"foo">).t
attest<"foo">(foo)
})
})
Expand Down
4 changes: 2 additions & 2 deletions ark/type/__tests__/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,8 @@ contextualize(() => {

it("allows string sybtyping", () => {
type({
foo: [/^foo/ as type.as<`foo${string}`>, "=", "foobar"],
bar: [/bar$/ as type.as<`${string}bar`>, "=", () => "foobar" as const]
foo: [/^foo/ as type.cast<`foo${string}`>, "=", "foobar"],
bar: [/bar$/ as type.cast<`${string}bar`>, "=", () => "foobar" as const]
})
})

Expand Down
2 changes: 1 addition & 1 deletion ark/type/__tests__/realWorld.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contextualize(() => {
const MockTimeStub = class TimeStub {}

const types = scope({
timeStub: ["instanceof", MockTimeStub] as type.as<TimeStub>,
timeStub: ["instanceof", MockTimeStub] as type.cast<TimeStub>,
account: "clientDocument&accountData",
clientDocument: {
"id?": "string",
Expand Down
2 changes: 1 addition & 1 deletion ark/type/__tests__/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ b.c.c must be an object (was missing)`)
"[string]": "value"
},
value: "primitive | record | value[]",
castValue: "value" as type.as<Value>
castValue: "value" as type.cast<Value>
}).export()

// TS type display blows up but it's equivalent to Value
Expand Down
2 changes: 1 addition & 1 deletion ark/type/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ export type inferPredicate<t, predicate> =
export type inferPipes<t, pipes extends Morph[]> =
pipes extends [infer head extends Morph, ...infer tail extends Morph[]] ?
inferPipes<
pipes[0] extends type.as<infer tPipe> ? inferPipe<t, tPipe>
pipes[0] extends type.cast<infer tPipe> ? inferPipe<t, tPipe>
: inferMorphOut<head> extends infer out ?
(In: distill.withAttributes.In<t>) => Out<out>
: never,
Expand Down
9 changes: 2 additions & 7 deletions ark/type/keywords/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,10 @@ export const keywords: Module<Ark> = ark.export()
export const type: TypeParser<{}> = ark.type as never

export declare namespace type {
export interface as<castTo> {
[inferred]?: castTo
export interface cast<to> {
[inferred]?: to
}

/**
* @deprecated Use type.as<castTo> instead
*/
export type cast<to> = as<to>

export type errors = ArkErrors

/** @ts-ignore cast variance */
Expand Down
20 changes: 10 additions & 10 deletions ark/type/methods/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,32 @@ interface Type<out t extends object = object, $ = {}> extends BaseType<t, $> {
* @example type({ foo: "string" }).get("foo") // Type<string>
*/
get<const k1 extends arkIndexableOf<t>, r = arkGet<t, k1>>(
k1: k1 | type.as<k1>
k1: k1 | type.cast<k1>
): instantiateType<r, $>
get<
const k1 extends arkIndexableOf<t>,
const k2 extends arkIndexableOf<arkGet<t, k1>>
>(
k1: k1 | type.as<k1>,
k2: k2 | type.as<k2>
k1: k1 | type.cast<k1>,
k2: k2 | type.cast<k2>
): instantiateType<arkGet<arkGet<t, k1>, k2>, $> extends infer r ? r : never
get<
const k1 extends arkIndexableOf<t>,
const k2 extends arkIndexableOf<arkGet<t, k1>>,
const k3 extends arkIndexableOf<arkGet<arkGet<t, k1>, k2>>,
r = arkGet<arkGet<arkGet<t, k1>, k2>, k3>
>(
k1: k1 | type.as<k1>,
k2: k2 | type.as<k2>,
k3: k3 | type.as<k3>
k1: k1 | type.cast<k1>,
k2: k2 | type.cast<k2>,
k3: k3 | type.cast<k3>
): instantiateType<r, $>

/**
* Create a copy of this `Type` with only the specified properties.
* @example type({ foo: "string", bar: "number" }).pick("foo") // Type<{ foo: string }>
*/
pick<const key extends arkKeyOf<t> = never>(
...keys: (key | type.as<key>)[]
...keys: (key | type.cast<key>)[]
): Type<
{
[k in keyof t as Extract<toArkKey<t, k>, key>]: t[k]
Expand All @@ -81,7 +81,7 @@ interface Type<out t extends object = object, $ = {}> extends BaseType<t, $> {
* @example type({ foo: "string", bar: "number" }).omit("foo") // Type<{ bar: number }>
*/
omit<const key extends arkKeyOf<t> = never>(
...keys: (key | type.as<key>)[]
...keys: (key | type.cast<key>)[]
): Type<
{
[k in keyof t as Exclude<toArkKey<t, k>, key>]: t[k]
Expand Down Expand Up @@ -203,15 +203,15 @@ type BaseMappedTypeProp<k extends Key, v> = merge<
BaseMappedPropInner,
{
key: k
value: type.as<v>
value: type.cast<v>
}
>

type OptionalMappedTypeProp<k extends Key, v> = merge<
OptionalMappedPropInner,
{
key: k
value: type.as<v>
value: type.cast<v>
default?: v
}
>
Expand Down
8 changes: 4 additions & 4 deletions ark/type/parser/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const parseObject = (def: object, ctx: BaseParseContext): BaseRoot => {

export type inferDefinition<def, $, args> =
[def] extends [anyOrNever] ? def
: def extends type.as<infer t> ?
: def extends type.cast<infer t> ?
// {} as a def is handled here since according to TS it extends { " arkInferred"?: t }.
// Unlike in TS however, ArkType object literals are constrained to object
// so we use that as the base type inferred when parsing {}.
Expand Down Expand Up @@ -98,7 +98,7 @@ export type validateDeclared<declared, def, $, args> =
: validateDefinition<def, $, args>

type validateInference<def, declared, $, args> =
def extends RegExp | type.as<unknown> | ThunkCast | TupleExpression ?
def extends RegExp | type.cast<unknown> | ThunkCast | TupleExpression ?
validateShallowInference<def, declared, $, args>
: def extends array ?
declared extends array ?
Expand Down Expand Up @@ -135,9 +135,9 @@ type declarationMismatch<def, declared, $, args> = {

// functions are ignored in validation so that cyclic thunk definitions can be
// inferred in scopes
type Terminal = type.as<unknown> | Fn
type Terminal = type.cast<unknown> | Fn

export type ThunkCast<t = unknown> = () => type.as<t>
export type ThunkCast<t = unknown> = () => type.cast<t>

type BadDefinitionType = Exclude<Primitive, string>

Expand Down

0 comments on commit 5dd8b6f

Please sign in to comment.