francis
Explore posts from serversAarktype
•Created by francis on 9/23/2024 in #questions
Is there currently a known issue with arktype + trpc compatibility?
This is with latest RC.12
This occurs when I do
.input(type(anything).assert)
. If I do something such as .input((i) => type(anything).assert(i))
, it works fine.
How on earth is it ending up with this
being undefined when it is run this way?4 replies
Aarktype
•Created by francis on 9/23/2024 in #questions
How would you define an object with optional keys, but require at least one of the keys to be filled
An example of a very basic schema:
I'd like this to accept values
{a: 'foo'}
, { b: 'foo' }
, and { a: 'foo', b: 'bar' }
, but fail on {}
. It would be a lovely bonus for the type to be inferred as { a: string, b?: string } | { a?: string, b: string }
as well. Is there a way to do this without explicitly specifying that inferred type as an arktype union using .or
?15 replies
Aarktype
•Created by francis on 9/21/2024 in #questions
Is there a recommended way to transform a type union into a discriminated union by adding a tag?
I'm not sure I'm describing this well, but I have a situation with two types that I am .or-ing together.
It all works, but I'm interacting with a library that needs a discriminated union to function properly for type narrowing, and I am not sure how to add a discriminated union tag based on which type in the Or resolved (if that makes sense?)
Here's a simple example to demonstrate:
I'd like to have this be resolve to e.g. { key: string, tag: 'first' } | { key: number, tag: 'second' } based on which branch in the union was followed.
I've tried it with this:
and it works - but seems inelegant. Is there a more idiomatic solution?
13 replies
Aarktype
•Created by francis on 9/21/2024 in #questions
Is there a way to do a case insensitive literal match?
I can pipe a
string.lower
to a lower-case literal but that seems complicated for this.27 replies
Aarktype
•Created by francis on 9/21/2024 in #questions
Is there a way to perform schema parsing generically?
My reasoning is to integrate Arktype with https://effect.website/
This most simple example fails:
This fails as the return type of
.assert
is finalizeDistillation<T, _distill<T, { endpoint: "out"; }>>
Is there a way to coerce this back to a T
? Or is there some constraint I need to put on the T
input to make this work?31 replies
Aarktype
•Created by francis on 8/25/2024 in #questions
vscode intellisense errors on the same type definition in some files, but not others
e.g. the following type:
In some files, this works fine. In others, in the exact same project, this causes the following error:
Any ideas for what might be going on?
7 replies
Aarktype
•Created by francis on 8/7/2024 in #questions
Are there examples of how to create mapped types?
e.g. a
{ [K in <SomeOtherType>]: SomeValueType }
type, where SomeValueType
is either a static type or is computed from K.
I currently have a very basic setup where I have:
This pattern allows me to generate, from a string array, a validator for an element in that array, a type for an element of the resulting string union type, and a type for a runtime object that lets me access the values analogous to an enum. And it's great!
My question is, I have the following type definition:
Is there a way to write an ArkType function that will generate a validator for this type (and the type definition itself), given the kitTypes
array?
My initial attempt is:
Obviously, this doesn't work, since the type information is completely lost. It actually works fine at runtime but the type is useless. Is there a way to do this without writing it out explicitly? (I have 5+ other enums that I'd like to process in a similar way)22 replies
Aarktype
•Created by francis on 8/2/2024 in #questions
How to accept a generic non-empty const string array for use in `type`?
Note: this may be a TS question, feel free to send me there instead.
I have the following type:
PgEnum<TValues extends [string, ...string[]]> { enumValues: TValues; }
I'm attempting to create a function which accepts this object and returns an arktype instance of the union of the strings in the array, which is const with well-known values.
If I call it directly from the const signature, e.g. type('===', obj.enumValues)
, it works fine. But I can't figure out how to accept this as a generic function parameter and use it in the body.
I have tried:
This fails on the spread argument with
Unwinding the generic produces different errors:
leads to A spread argument must either have a tuple type or be passed to a rest parameter.
Replacing string[]
with [string, ...string[]]
leads to the same issue.
I suspect the two generic parameters is the closer approach, but I have no idea what this conform<>
error is, or how to begin to address it.3 replies