Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intersect with discriminated type #1237

Open
Kal-Aster opened this issue Dec 25, 2024 · 1 comment
Open

Intersect with discriminated type #1237

Kal-Aster opened this issue Dec 25, 2024 · 1 comment
Assignees
Labels
bug Something isn't working confirmed The maintainers of the repo would like to address this

Comments

@Kal-Aster
Copy link

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?

@Kal-Aster Kal-Aster added the bug Something isn't working label Dec 25, 2024
@github-project-automation github-project-automation bot moved this to To do in arktypeio Dec 25, 2024
@ssalbdivad
Copy link
Member

ssalbdivad commented Jan 3, 2025

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.

@ssalbdivad ssalbdivad added the confirmed The maintainers of the repo would like to address this label Jan 3, 2025
@ssalbdivad ssalbdivad moved this from To do to Backlog in arktypeio Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working confirmed The maintainers of the repo would like to address this
Projects
Status: Backlog
Development

No branches or pull requests

2 participants