jacksteamdev
jacksteamdev
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
I think what I was looking for was essentially if ... else support for string matching:
// if string is a, b, or c
type('"a"|"b"|"c"')
.pipe(doSomething)
// otherwise if it's a string
.or(type('string').pipe(doSomethingElse));
// if string is a, b, or c
type('"a"|"b"|"c"')
.pipe(doSomething)
// otherwise if it's a string
.or(type('string').pipe(doSomethingElse));
Maybe subset matching... but I got there in the end.
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
@TizzySaurus Thanks for the help! I wasn't able to get type("keyof", weightAliases) working, it requires weightAliases to be a Type, as is it can't resolve the property values. I was able to reduce casting with type.enumerated(...weightNames) and an Object.keys wrapper:
const keys = <T extends Record<string, unknown>>(obj: T) => Object.keys(obj) as (keyof T)[];
const keys = <T extends Record<string, unknown>>(obj: T) => Object.keys(obj) as (keyof T)[];
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
Right now I'm wrapping it in a function:
export function normalizedUnit(unit: {
amount: number;
unit: string;
}): ReturnType<typeof normalizedUnitUnion> | ReturnType<typeof namedUnit> {
const normalized = normalizedUnitUnion(unit);
if (normalized instanceof type.errors) {
return namedUnit(unit);
}
return normalized;
}
export function normalizedUnit(unit: {
amount: number;
unit: string;
}): ReturnType<typeof normalizedUnitUnion> | ReturnType<typeof namedUnit> {
const normalized = normalizedUnitUnion(unit);
if (normalized instanceof type.errors) {
return namedUnit(unit);
}
return normalized;
}
Which works, but I was curious if there was a better way to do it.
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
It's not exactly a minimal repro, sorry 😓
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
This is toStringUnion:
const weightAliases = {
oz: 'oz',
lb: 'lb',
lbs: 'lb',
pound: 'lb',
pounds: 'lb',
// other...
} as const satisfies Record<string, StandardWeightUnit>;

const weightAliasList = Object.keys(weightAliases) as StandardWeightUnit[];

const toStringUnion = (aliasList: string[]) =>
aliasList.map((str) => `"${str}"`).join(' | ') as '"string"';
const weightAliases = {
oz: 'oz',
lb: 'lb',
lbs: 'lb',
pound: 'lb',
pounds: 'lb',
// other...
} as const satisfies Record<string, StandardWeightUnit>;

const weightAliasList = Object.keys(weightAliases) as StandardWeightUnit[];

const toStringUnion = (aliasList: string[]) =>
aliasList.map((str) => `"${str}"`).join(' | ') as '"string"';
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
Hi @TizzySaurus ! My goal is to normalize recipe ingredient measurements before writing to the db. Later I have an SQL function that combines ingredients from different recipes into a grocery list.
16 replies
Aarktype
Created by jacksteamdev on 8/8/2023 in #questions
How to validate that a value is an instance of a class
do you want an issue for it, or do you think it's covered already?
23 replies
Aarktype
Created by jacksteamdev on 8/8/2023 in #questions
How to validate that a value is an instance of a class
lol, at least there's a workaround, no matter how ugly 👹
23 replies
Aarktype
Created by jacksteamdev on 8/8/2023 in #questions
How to validate that a value is an instance of a class
here's a fun one with private properties: https://stackblitz.com/edit/arktype-bug-rxgdqa?file=demo.ts
import { type } from 'arktype';

class Foo {
private bar = 'baz';
}

function fn(foo: Foo) {
// ....
}

const Foo1 = type(['instanceof', Foo]).assert(new Foo());
fn(Foo1); // Type Error: Argument of type '{}' is not assignable to parameter of type 'Foo'.
// Property 'bar' is missing in type '{}' but required in type 'Foo'.(2345)

const Foo2 = type([
'unknown',
'|>',
(value: unknown): Foo => {
if (value instanceof Foo) return value;
throw new Error('value must be an Foo instance');
},
]).assert(new Foo());
fn(Foo2);
import { type } from 'arktype';

class Foo {
private bar = 'baz';
}

function fn(foo: Foo) {
// ....
}

const Foo1 = type(['instanceof', Foo]).assert(new Foo());
fn(Foo1); // Type Error: Argument of type '{}' is not assignable to parameter of type 'Foo'.
// Property 'bar' is missing in type '{}' but required in type 'Foo'.(2345)

const Foo2 = type([
'unknown',
'|>',
(value: unknown): Foo => {
if (value instanceof Foo) return value;
throw new Error('value must be an Foo instance');
},
]).assert(new Foo());
fn(Foo2);
23 replies
Aarktype
Created by jacksteamdev on 8/8/2023 in #questions
How to validate that a value is an instance of a class
Nice, thanks!
23 replies
Aarktype
Created by jacksteamdev on 8/8/2023 in #questions
How to validate that a value is an instance of a class
I came up with something like this:
type(["any", "|>",
(x): SomeClass => {
if (x instanceof SomeClass) return self;
throw new Error("must be a SomeClass instance");
}
]);
type(["any", "|>",
(x): SomeClass => {
if (x instanceof SomeClass) return self;
throw new Error("must be a SomeClass instance");
}
]);
23 replies