arktype icon indicating copy to clipboard operation
arktype copied to clipboard

Incorrect discriminated union processing

Open TizzySaurus opened this issue 1 year ago • 0 comments

Report a bug

🔎 Search Terms

boolean unioned with a recursive discriminated union, recursion, recursive discriminated union

🧩 Context

  • ArkType version: 2.0-dev.26
  • TypeScript version (5.1+): 5.5
  • Other context you think may be relevant (JS flavor, OS, etc.): N/A

🧑‍💻 Repro

import { scope } from "arktype"

const $ = scope({
    TypeWithNoKeywords: { type: "'boolean'|'null'" },
    TypeWithKeywords: "ArraySchema|ObjectSchema", // without both ArraySchema and ObjectSchema there's no error
    // "#BaseSchema": "TypeWithNoKeywords|boolean", // errors even with union reversed
    "#BaseSchema": "boolean|TypeWithNoKeywords", // without the `boolean` there's no error (even if still union such as `string|TypeWithNoKeywords`)
    ArraySchema: {
        "additionalItems?": "BaseSchema", // without this recursion there's no error
        type: "'array'"
    },
    // If `ObjectSchema` doesn't have `type` key there's no error
    ObjectSchema: {
        type: "'object'"
    }
})
export const JsonSchema = $.export() // TypeError: Cannot use 'in' operator to search for 'type' in false

The validateObjectLiteral type doesn't account for the => operator and when it discovers an unaccounted for operator it assumes it's the = (default) operator. I'm guessing this is the cause.

TizzySaurus avatar Jul 02 '24 14:07 TizzySaurus