Graff
Graff
Aarktype
Created by Graff on 1/21/2025 in #questions
Recommended syntax for Higher Order Functions?
Hi all. Just found the project and started playing with it in a sandbox. I'm trying to create a module that will take an arktype as an input and return an updated arktype as the output while conserving the type safety. EG:
import { type as arkType } from "arktype";

export const uuid = arkType(
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,
).brand("UID");

const hasUuid = arkType({
uuid,
});

function generateUUID(): typeof uuid.infer {
const n = Bun.randomUUIDv7();
const out = uuid(n);
if (out instanceof arkType.errors) {
throw new Error(`Problem generating random uid: ${out.summary}`);
}
return out;
}

// what should I use here?
function appendUuidToSchema<const def>(schema: arkType.validate<def, object>) {
return hasUuid.and(schema);
}

// testing
const inputArkType = arkType({
environment: arkType.enumerated("dev", "qa", "local"),
});

const arktypeFromFunction = appendUuidToSchema(inputArkType);
type X = typeof arktypeFromFunction.infer;
/*
TS2322: Type
{ uuid: Brand<string, 'UID'>; environment: string; }
is not assignable to type
{ uuid: Brand<string, 'UID'>; } & ' anyOrNever' & Type<{ environment: 'dev' | 'qa' | 'local'; }, {}>
*/
const xx: X = {
uuid: generateUUID(),
environment: "dev",
};

const arktypeFromNativeMethods = hasUuid.and(inputArkType);
type Y = typeof arktypeFromNativeMethods.infer;
const yy: Y = {
uuid: generateUUID(),
environment: "dev",
};
import { type as arkType } from "arktype";

export const uuid = arkType(
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,
).brand("UID");

const hasUuid = arkType({
uuid,
});

function generateUUID(): typeof uuid.infer {
const n = Bun.randomUUIDv7();
const out = uuid(n);
if (out instanceof arkType.errors) {
throw new Error(`Problem generating random uid: ${out.summary}`);
}
return out;
}

// what should I use here?
function appendUuidToSchema<const def>(schema: arkType.validate<def, object>) {
return hasUuid.and(schema);
}

// testing
const inputArkType = arkType({
environment: arkType.enumerated("dev", "qa", "local"),
});

const arktypeFromFunction = appendUuidToSchema(inputArkType);
type X = typeof arktypeFromFunction.infer;
/*
TS2322: Type
{ uuid: Brand<string, 'UID'>; environment: string; }
is not assignable to type
{ uuid: Brand<string, 'UID'>; } & ' anyOrNever' & Type<{ environment: 'dev' | 'qa' | 'local'; }, {}>
*/
const xx: X = {
uuid: generateUUID(),
environment: "dev",
};

const arktypeFromNativeMethods = hasUuid.and(inputArkType);
type Y = typeof arktypeFromNativeMethods.infer;
const yy: Y = {
uuid: generateUUID(),
environment: "dev",
};
Any suggestions to make xx == yy ?
1 replies