Skip to content

Add type error to optional index signature expression like [sizes]? #1525

Closed

Description

@Sam-C137

Report a bug

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.

🔎 Search Terms

  • generic
  • optional

🧩 Context

  • ArkType version: 2.1.23
  • TypeScript version (5.1+): 5.9.3

🧑‍💻 Repro

Playground Link: here

import { type } from "arktype"

const createImageVariants = type("<sizes extends string>", {
	"[sizes]?": {
		url: "string.url",
		"width?": "number | string.numeric.parse | null",
		"height?": "number | string.numeric.parse | null",
	}
})

const avatar = createImageVariants("'small'|'medium'|'large'")
//    ^?

typeof avatar

const avatar: Type<{
    "[sizes]"?: {
        url: string;
        width?: number | ((In: string) => To<number>) | null;
        height?: number | ((In: string) => To<number>) | null;
    };
}, {}>
Expected
const avatar: Type<{
    small?: {
        url: string;
        width?: number | ((In: string) => To<number>) | null;
        height?: number | ((In: string) => To<number>) | null;
    };
    medium?: {
        url: string;
        width?: number | ((In: string) => To<number>) | null;
        height?: number | ((In: string) => To<number>) | null;
    };
    large?: {
        url: string;
        width?: number | ((In: string) => To<number>) | null;
        height?: number | ((In: string) => To<number>) | null;
    };
}, {}>
Workaround
import { type } from "arktype"

type AvatarImpl<
	T extends string = "small" | "medium" | "large",
> = Partial<
	Record<
		T,
		{
			url: string;
			width: number | null;
			height: number | null;
		}
	>
>;

const avatar = createImageVariants("'small'|'medium'|'large'").as<AvatarImpl>()
//      ^?

Activity

  1. ssalbdivad commented on Oct 27, 2025

    @ssalbdivad
    Member

    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:

    import { type } from "arktype"
    
    const createImageVariants = type("<sizes extends string>", {
    	"[sizes]": {
    		url: "string.url",
    		"width?": "number | string.numeric.parse | null",
    		"height?": "number | string.numeric.parse | null",
    	}
    })
    
    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.

  2. 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
  3. Sam-C137 commented on Oct 27, 2025

    @Sam-C137
    Author

    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:

    import { type } from "arktype"

    const createImageVariants = type("", {
    "[sizes]": {
    url: "string.url",
    "width?": "number | string.numeric.parse | null",
    "height?": "number | string.numeric.parse | null",
    }
    })

    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?

  4. ssalbdivad commented on Oct 27, 2025

    @ssalbdivad
    Member

    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.

  5. Sam-C137 commented on Oct 27, 2025

    @Sam-C137
    Author

    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"
    
    const createImageVariants = type("<sizes extends string>", {
    	"[sizes]": {
    		url: "string.url",
    		"width?": "number | string.numeric.parse | null",
    		"height?": "number | string.numeric.parse | null",
    	}
    })
    
    const user = 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.

  6. ssalbdivad commented on Oct 27, 2025

    @ssalbdivad
    Member

    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:

    import { type } from "arktype"
    
    const createImageVariants = type("<sizes extends string>", {
    	"[sizes]": {
    		url: "string.url",
    		"width?": "number | string.numeric.parse | null",
    		"height?": "number | string.numeric.parse | null",
    	}
    })
    
    const avatar = createImageVariants("'small'|'medium'|'large'").partial()
    
    const user = type({
        name: "string",
        avatar
    })

    Here's a deepPartial utility you can use if that's the behavior you want:

    import { type } from "arktype"
    
    const user = type({
    	name: "string",
    	nested: {
    		inner: "number"
    	}
    })
    
    type deepPartial<o> = {
    	[k in keyof o]?: o[k] extends (infer U)[] ? deepPartial<U>[]
    	: o[k] extends object | undefined ? deepPartial<o[k]>
    	: o[k]
    } & unknown
    
    function deepPartial<o extends type<object>>(o: o): type<deepPartial<o["t"]>>
    function deepPartial(o: type<Record<string, unknown>>): unknown {
    	return o.map(prop =>
    		prop.value.ifExtends("object") ?
    			{ ...prop, kind: "optional", value: deepPartial(prop.value as never) }
    		:	{ ...prop, kind: "optional" }
    	)
    }
    
    // Type<{ name?: string, nested?: { inner?: number } }>
    const deepPartialUser = deepPartial(user)
    
    // { name?: string, nested?: { inner?: number } }
    console.log(deepPartialUser.expression)
  7. Sam-C137 commented on Oct 27, 2025

    @Sam-C137
    Author

    Thanks David, you're awsome 🙌

  8. Bas950 commented on Feb 16, 2026

    @Bas950
    Contributor

    @ssalbdivad @Sam-C137
    Typescript does something support a syntax like this:

    type Locales = "en" | "nl" | "de"
    
    type LocalizedText = {
      [K in Locales]? : string
    }

    It creates the following type:

    type LocalizedText = {
        en?: string | undefined;
        nl?: string | undefined;
        de?: string | undefined;
    }

    So maybe arktype could too?

  9. ssalbdivad commented on Nov 18, 2026

    @ssalbdivad
    Member

    Fixed in 3.0.0-beta.0 (#1728). Try it with npm i arktype@next.

  10. moved this from To do to Done (merged or closed) in arktypeioon Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

  • Status
    Done (merged or closed)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions