Skip to content

Config for values that fallback to a default #1390

Closed

Description

@ssalbdivad

undefined should be implied by exactOptionalPropertyTypes to avoid this unintuitive current behavior:

const myObj = type({
  key: "number = 5",
});

// { key: 5 }
const omittedResult = myObj({});

// { key: undefined }
const undefinedResult = myObj({ key: undefined });

Could also consider supporting other values like null, "" etc., possibly via an arbitrary shouldDefault: (value: unknown) => boolean function.

EDIT: A thought about how this could be done that would fit will into the type system:

// sort of parallel to onUndeclaredKey
const o = type({
    "-": "'' | null | undefined",
    "foo?": "string"
})

// equivalent to
const t = type.object.pipe(o => flatMorph(o, (k, v) => v === null || v === undefined || v === "" ? [] : [k, v])).to({ "foo?": "string" })

Should be addressed alongside #1283

Activity

  1. moved this from To do to Backlog in arktypeioon Mar 24, 2025
  2. ssalbdivad commented on Nov 18, 2026

    @ssalbdivad
    MemberAuthor

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

  3. moved this from Backlog 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      Done (merged or closed)

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions