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
34 changes: 33 additions & 1 deletion ark/type/__tests__/realWorld.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type ArkErrors,
type StandardSchemaV1
} from "@ark/schema"
import { scope, type, type Module } from "arktype"
import { scope, type, type Module, type Type } from "arktype"
import type { Out, To } from "arktype/internal/attributes.ts"

declare class TimeStub {
Expand Down Expand Up @@ -1366,4 +1366,36 @@ Right: { x: number, y: number, + (undeclared): delete }`)
'{ masterItem: true, minPrice: number % 1 & >= 0, qualities: (1 | 2 | 3 | 4 | 5)[] | [null], short: string, skin: string, type: "skin", souvenirAvailable: boolean = false, stattrakAvailable: boolean = false, + (undeclared): reject } | { short: string, skin: string, type: "skin", souvenir: boolean = false, stattrak: boolean = false, quality?: 1 | 2 | 3 | 4 | 5, weight?: number, + (undeclared): reject }'
)
})

it("allows inferring a schema's type argument in a generic wrapper function when the type uses Default", () => {
function someFunction<TSchema extends Record<string, any>>(
schema: Type<TSchema, {}>
): (typeof schema)["infer"] {
const someData = { hello: "world" }
return schema.assert(someData)
}

const schema = type({
hello: type("string").pipe(s => s === "world"),
goodbye: "string='blah'"
})

someFunction(schema)
})

it("allows inferring a schema in a generic wrapper function when the type uses Default", () => {
function someFunction<schema extends type.Any<Record<string, any>>>(
schema: schema
) {
const someData = { hello: "world" }
return schema.assert(someData)
}

const schema = type({
hello: type("string").pipe(s => s === "world"),
goodbye: "string='blah'"
})

someFunction(schema)
})
})
5 changes: 4 additions & 1 deletion ark/type/variants/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,10 @@ interface Type<out t = unknown, $ = {}>
): reduceOut

/** The Type's [StandardSchema](https://github.com/standard-schema/standard-schema) properties */
"~standard": StandardSchemaV1.ArkTypeProps<this["inferIn"], this["inferOut"]>
"~standard": StandardSchemaV1.ArkTypeProps<
this["inferIn"] extends infer _ ? _ : never,
this["inferOut"] extends infer _ ? _ : never
>

// deprecate Function methods so they are deprioritized as suggestions

Expand Down