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)12 Replies
Mapped types aren't supported yet:
https://github.com/arktypeio/arktype/issues/584
But you can use index signatures if the value doesn't depend on the key:
https://github.com/arktypeio/arktype/blob/16f254a57715d2b446c94a269cabb32bd36cfec3/ark/type/__tests__/objectLiteral.test.ts#L222
aha, great place to start looking, thank you!
immediate follow up question: is there a way to generate the enumerable union from my other union arktype instance?
i.e. is there a way to generate
"'test_only' | 'test_self_serve' | 'test_guided_audit'"
from my kitTypeValidator
- and if so, would it then be safe to use a template string literal to put a ?
on the end?If you have an index signature you can't make it optional, but you could refer to your union directly, at least in a scope
or is there a way to define a Record in arktype without using object syntax? I assume I have to turn it into an intermediate string since only strings / symbols are property keys
ark.Record
ark
is a module containing all the builtin keywordsah, right, thank you. I forgot that index signatures are always optional (or rather, they're weird, and I am not entirely sure I like how typescript does it but I also can't think of a better way)
turning on
noUncheckedIndexAccess
is better for type safety but breaks so many libraries that I gave up on it and define all the records I use internal to our application as Record<key, value | undefined>
this is one of the reasons I like mapped types, with mapped types you can enforce that all keys are presentIt won't be inferred as optional in that case but you can
.partial()
it to make it optional
Index signatures would enforce all the kys are present if it is an enumerable list(that's how it works in TS so I mirrored that behavior, even though it's a bit weird IMO)
It does, if you pass an enumerable union to a Record those keys will be required
jk, I forgot that part, yeah
I must be doing something very wrong but I can't figure out how to get
ark.Record
to work at all? even following the test case I found in the arktype repo gets me different results.
There was a bug with generic inference in some recent versions is it up to date?
I updated a couple days ago
ah, new version, I'll update and check. thanks!