TizzySaurus
TizzySaurus
Aarktype
Created by papy0977 on 1/20/2025 in #questions
Hi,
And for the second, wouldn't just calling the type do that? I.e. foo()/foo({})
4 replies
Aarktype
Created by papy0977 on 1/20/2025 in #questions
Hi,
You can try foo: type("string[]").default(["bar"])
4 replies
Aarktype
Created by Tony on 12/19/2024 in #questions
Optional required pairs?
One way would be to use .narrow(), which has the added benefit of being able to specify custom errors:
const props = type({ ... }).narrow((obj, ctx) =>
"propB" in obj === "propC" in obj
? true
: ctx.mustBe({
expected:
"an object that either includes both or neither of 'propB' and 'propC'",
})
);
const props = type({ ... }).narrow((obj, ctx) =>
"propB" in obj === "propC" in obj
? true
: ctx.mustBe({
expected:
"an object that either includes both or neither of 'propB' and 'propC'",
})
);
If you just want the default error messages though do something like what @ArkDavid suggested below
3 replies
Aarktype
Created by JesusTheHun on 11/19/2024 in #questions
`narrow` vs `satisfying`
E.g.:
type("string.numeric.parse").narrow(x => {...}) // `x` is a number
type("string.numeric.parse").satisfying(x => {...}) // `x` is a string
type("string.numeric.parse").narrow(x => {...}) // `x` is a number
type("string.numeric.parse").satisfying(x => {...}) // `x` is a string
11 replies
Aarktype
Created by JesusTheHun on 11/19/2024 in #questions
`narrow` vs `satisfying`
There'd need to be some form of transform, yes. Not necessarily a .pipe() explicitly though.
11 replies
Aarktype
Created by JesusTheHun on 11/19/2024 in #questions
`narrow` vs `satisfying`
narrow applies the constraint to the output, whereas satisfying applies the constraint to the input
11 replies
Aarktype
Created by JesusTheHun on 11/19/2024 in #questions
`narrow` vs `satisfying`
narrow(predicate: Predicate): BaseRoot {
return this.constrainOut("predicate", predicate)
}

satisfying(predicate: Predicate): BaseRoot {
return this.constrain("predicate", predicate)
}
narrow(predicate: Predicate): BaseRoot {
return this.constrainOut("predicate", predicate)
}

satisfying(predicate: Predicate): BaseRoot {
return this.constrain("predicate", predicate)
}
this is the difference in the source code
11 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
Fwiw there's also
const nameUnion = type("keyof", weightAliases)
const nameUnion = type("keyof", weightAliases)
iirc
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
But in theory you could do something like....
const weightAliases = {
oz: 'oz',
lb: 'lb',
lbs: 'lb',
pound: 'lb',
pounds: 'lb',
// other...
} as const satisfies Record<string, StandardWeightUnit>;

const weightNames = Object.keys(weightAliases) as StandardWeightName[];
const weightUnits = Object.values(weightAliases) as StandardWeightUnit[];

const nameUnion = type.enumerated(weightNames);
const unitUnion = type.enumerated(weightUnits);


const normalizedUnit = type({amount: "number", unit: nameUnion.or(unitUnion)});
const weightAliases = {
oz: 'oz',
lb: 'lb',
lbs: 'lb',
pound: 'lb',
pounds: 'lb',
// other...
} as const satisfies Record<string, StandardWeightUnit>;

const weightNames = Object.keys(weightAliases) as StandardWeightName[];
const weightUnits = Object.values(weightAliases) as StandardWeightUnit[];

const nameUnion = type.enumerated(weightNames);
const unitUnion = type.enumerated(weightUnits);


const normalizedUnit = type({amount: "number", unit: nameUnion.or(unitUnion)});
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
Your type also seems to be wrong because weightAliases is Record<string, StandardWeightUnit>, but then you do Object.keys(weightAliases) as StandardWeightUnit[]. But the keys are string, not StandardWeightUnit. It's the values that are StandardWeightUnit
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
What is normalizedUnitUnion and namedUnit?
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
There's also type.enumerated([1, 2, 3]) iirc
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
toStringUnion should just be type("...", aliasList). I wouldn't even wrap it in a function since then you loose the type inference -- just use it directly
const t = type("...", [1, 2, 3])
type t = t.infer // 1 | 2 | 3
const t = type("...", [1, 2, 3])
type t = t.infer // 1 | 2 | 3
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
It's not entirely clear to me what you're trying to achieve though
16 replies
Aarktype
Created by jacksteamdev on 11/9/2024 in #questions
Union Fallthrough?
Are you aware of type("...", arrOfTypes)? That may be what you're looking for with toStringUnion
16 replies
Aarktype
Created by terinjokes on 11/4/2024 in #questions
morphing to typescript type
Err, you can do. Might be worth waiting for David to confirm it is indeed a bug.
24 replies
Aarktype
Created by terinjokes on 11/4/2024 in #questions
morphing to typescript type
Tl;Dr; I'm fairly certain this is a bug in ArkType (tbc by David), so thanks for reporting this 🙂
24 replies
Aarktype
Created by terinjokes on 11/4/2024 in #questions
morphing to typescript type
@ssalbdivad Are you able to confirm that this is the case (i.e. this is unintentional)? Context: the inferred type of a morph that returns an object with valueOf: () => never becomes valueOf: never (instead of keeping valueOf as a function, as it does for when e.g. valueOf: () => number)
24 replies
Aarktype
Created by terinjokes on 11/4/2024 in #questions
morphing to typescript type
I assume this is because () => never extends InferredMorph, which I don't think it should.
24 replies
Aarktype
Created by terinjokes on 11/4/2024 in #questions
morphing to typescript type
No description
24 replies