Repository navigation
Closed
Description
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.0I 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?
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone (merged or closed)
Activity
ssalbdivad commentedon 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.Christopher96u commentedon Apr 10, 2025
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
ssalbdivad commentedon Apr 13, 2025
@Christopher96u Here's one way you could do it:
fix(schema): identify an alias by its context id
fix(schema): instantiate recursive generics once per argument set
docs: drop the claim that recursive generics are unavailable
test(type): trim cyclic.test to one case per behavior
docs(type): add CHANGELOG entries for cyclic data and recursive generics
ssalbdivad commentedon Nov 18, 2026
Fixed in 3.0.0-beta.0 (#1728). Try it with
npm i arktype@next.