You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add type error to optional index signature expression like [sizes]? #1525
Creating a generic type where the type argument becomes an object key works fine but we loose type inference when this key is optional. Not sure if this is a bug or a limitation of TypeScript.
Thanks for the report! The combination of an index signature and a ? is not supported in TypeScript and also not currently supported in ArkType, though I can see the potential benefit in cases like this where the signature should expand to a set of literal props.
The most straightforward workaround to this is to just use an index signature and call .partial on the result afterwards:
changed the title [-]Generic types with dynamic optional object keys loose type inference[/-][+]Add type error to optional index signature expression like `[sizes]?`[/+]on Oct 27, 2025
Thanks for the report! The combination of an index signature and a ? is not supported in TypeScript and also not currently supported in ArkType, though I can see the potential benefit in cases like this where the signature should expand to a set of literal props.
The most straightforward workaround to this is to just use an index signature and call .partial on the result afterwards:
const avatar = createImageVariants("'small'|'medium'|'large'").partial()
I will keep this issue open to track providing a type error on this type of key expression.
Thanks for the reply! And yes you are right, the combination of an index signature and a ? is not supported in TypeScript but currently arktype does not throw an error, does it mean the schema is bypassed?
For now it is just treated as a literal optional key named "sizes" as reflected by the types.
This behavior is not necessarily incorrect, but I'll think about whether it would be better to throw an error here and require extra escape characters.
That makes sense. Also I tried your straightforward approach in the playground and it works as expected but partial breaks when the avatar object is nested eg:
import{type}from"arktype"constcreateImageVariants=type("<sizes extends string>",{"[sizes]": {url: "string.url","width?": "number | string.numeric.parse | null","height?": "number | string.numeric.parse | null",}})constuser=type({name: "string",avatar: createImageVariants("'small'|'medium'|'large'")// ^? the keys of this object are no longer partialized}).partial()
The keys of the object are all partialized except the generic key and its decendants. Normally the expected behavior of arktype is a deep partial on all keys, again not sure if this is a bug or expected behavior.
Activity
ssalbdivad commentedon Oct 27, 2025
Thanks for the report! The combination of an index signature and a
?is not supported in TypeScript and also not currently supported in ArkType, though I can see the potential benefit in cases like this where the signature should expand to a set of literal props.The most straightforward workaround to this is to just use an index signature and call
.partialon the result afterwards:I will keep this issue open to track providing a type error on this type of key expression.
[-]Generic types with dynamic optional object keys loose type inference[/-][+]Add type error to optional index signature expression like `[sizes]?`[/+]Sam-C137 commentedon Oct 27, 2025
Thanks for the reply! And yes you are right, the combination of an index signature and a
?is not supported in TypeScript but currently arktype does not throw an error, does it mean the schema is bypassed?ssalbdivad commentedon Oct 27, 2025
For now it is just treated as a literal optional key named
"sizes"as reflected by the types.This behavior is not necessarily incorrect, but I'll think about whether it would be better to throw an error here and require extra escape characters.
Sam-C137 commentedon Oct 27, 2025
That makes sense. Also I tried your straightforward approach in the playground and it works as expected but
partialbreaks when the avatar object is nested eg:The keys of the object are all partialized except the generic key and its decendants. Normally the expected behavior of arktype is a deep partial on all keys, again not sure if this is a bug or expected behavior.
ssalbdivad commentedon Oct 27, 2025
In the case of
partial/Partial<>as well, the behavior mirrors TypeScript and only applies optionality shallowly.You would just reference the partial object as a key if you want to nest it further:
Here's a deepPartial utility you can use if that's the behavior you want:
Sam-C137 commentedon Oct 27, 2025
Thanks David, you're awsome 🙌
Bas950 commentedon Feb 16, 2026
@ssalbdivad @Sam-C137
Typescript does something support a syntax like this:
It creates the following type:
So maybe arktype could too?
feat(type): throw for an optional index signature like "[k]?"
ssalbdivad commentedon Nov 18, 2026
Fixed in 3.0.0-beta.0 (#1728). Try it with
npm i arktype@next.