Skip to content

Intersect with discriminated type #1237

Closed

Description

@Kal-Aster

Report a bug

🔎 Search Terms

"discriminated"
"intersect"
"ParseError: T must be assignable to object"

🧩 Context

  • ArkType version: 2.0.0-rc.30
  • TypeScript version (5.1+): 5.7.2
  • Other context you think may be relevant (JS flavor, OS, etc.):

🧑‍💻 Repro

import { scope } from "arktype";

scope({
    AEditing: {
        edits: {
            a: "number"
        },
        nodes: "(Omit<NodeA, 'editableA'> | Omit<NodeB, 'editableA' | 'edits'>)[]"
    },
    NonEditing: {
        edits: "undefined?",
        nodes: "(NodeA | NodeB)[]"
    },
    NodeA: {
        type: "'a'",
        editableA: "number"
    },
    BaseNodeB: {
        type: "'b'",
        editableA: "number"
    },
    Editing: "AEditing | NonEditing",
    NodeB: "BaseNodeB & Editing",
    Node: "NodeA | NodeB"
}).export();

I get the following error:

D:\***\node_modules\@ark\util\out\errors.js:5    throw new ctor(message);
          ^

ParseError: T must be assignable to object (was $AEditing&intersection250 | { editableA: number, nodes: ($NodeB | { editableA: number, type: "a" })[], type: "b", edits?: undefined })    
    at throwError (D:\***\node_modules\@ark\util\out\errors.js:5:11)
    at throwParseError (D:\***\node_modules\@ark\util\out\errors.js:10:43)
    at <anonymous> (D:\***\node_modules\@ark\schema\out\generic.js:21:21)
    at <anonymous> (D:\***\node_modules\@ark\util\out\flatMorph.js:4:39)
    at Array.flatMap (<anonymous>)
    at flatMorph (D:\***\node_modules\@ark\util\out\flatMorph.js:3:39)
    at GenericRoot.<anonymous> (D:\***\node_modules\@ark\schema\out\generic.js:18:30)
    at parseGenericInstantiation (D:\***\node_modules\arktype\out\parser\shift\operand\unenclosed.js:18:12)    at maybeParseReference (D:\***\node_modules\arktype\out\parser\shift\operand\unenclosed.js:40:16)      
    at unenclosedToNode (D:\***\node_modules\arktype\out\parser\shift\operand\unenclosed.js:20:40)

Node.js v22.12.0

I need the validation for a type that can have this "edits" property and based on the presence of this property and its actual structure (it could be another type of edits, in the actual usage) I need to narrow the type of "nodes".

In TS it works:

type AEditing = {
    edits: {
        a: number
    },
    nodes: (Omit<NodeA, "editableA"> | Omit<NodeB, "editableA">)[]
}
type NonEditing = {
    edits?: undefined,
    nodes: (NodeA | NodeB)[]
}
type NodeA = {
    type: "a",
    editableA: number
}
type NodeB = {
    type: "b",
    editableA: number
} & (AEditing | NonEditing);
type Node = NodeA | NodeB;

const node: Node = {
    type: "b",
    editableA: 12,
    edits: {
        a: 23
    },
    nodes: [
        {
            type: "b",
            // @ts-expect-error
            editableA: 23,
            nodes: []
        }
    ]
}

I'm new to this library: is this actually a bug or am I doing something wrong?

Activity

  1. added
    bug
    on Dec 25, 2024
  2. ssalbdivad commented on Jan 3, 2025

    @ssalbdivad
    Member

    This issue is similar to #1188. Unfortunately, there are still a few issues related to complex cyclic unions.

    Handling these precisely will require a more significant refactor as outlined in #1026.

    In the meantime, the best workaround would be to just avoid the problematic cyclic reference directly in the type and instead recurse by invoking the type directly from a .narrow.

  3. added
    confirmed
    on Jan 3, 2025
  4. moved this from To do to Backlog in arktypeioon Jan 3, 2025
  5. Christopher96u commented on Apr 10, 2025

    @Christopher96u

    This issue is similar to #1188. Unfortunately, there are still a few issues related to complex cyclic unions.

    Handling these precisely will require a more significant refactor as outlined in #1026.

    In the meantime, the best workaround would be to just avoid the problematic cyclic reference directly in the type and instead recurse by invoking the type directly from a .narrow.

    Could you make an example of this David? I'm not sure how to do this. On my case how should I use the narrow? I have an array of cms components where all of them have the type prop to know which component needs to be rendered and 90% of them have a prop called items/content which is an array of cms components too(cyclic) how should I use the narrow in this scenario? Thanks in advance

  6. ssalbdivad commented on Apr 13, 2025

    @ssalbdivad
    Member

    @Christopher96u Here's one way you could do it:

    const T = type({
    	cyclicKey: () =>
    		type.unknown.pipe((data, ctx) => T.internal(data, ctx)).as<typeof T.t>()
    })
    
    // cyclicKey.cyclicKey.cyclicKey must be present (was missing)
    T.assert({ cyclicKey: { cyclicKey: {} } })
  7. ssalbdivad commented on Nov 18, 2026

    @ssalbdivad
    Member

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

  8. 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

Labels

bugconfirmed

Type

No type

Projects

  • Status
    Done (merged or closed)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions