Skip to content

recursive union rejects when nullable #1284

Closed

Description

@trsaunders

Report a bug

I've found another unexpected behaviour with some complex type setups. The issue only occurs when null is part of the union, and when it is a recursive one.

🔎 Search Terms

  • recursive nullable union

🧩 Context

  System:
    OS: macOS 14.2.1
    CPU: (10) arm64 Apple M2 Pro
    Memory: 737.78 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.18.1 - ~/.nvm/versions/node/v20.18.1/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v20.18.1/bin/npm
  npmPackages:
    @ark/attest: ^0.30.0 => 0.30.0 
    @ark/fs:  0.26.0 
    @ark/schema:  0.39.0 
    @ark/util:  0.26.0 
    arktype: ^2.0.4 => 2.0.4 
    typescript: ^5.7.3 => 5.7.3 

🧑‍💻 Repro

import { scope } from "arktype";

export const ruleset = scope({
    TypeX: {
        id: "string",
        "+": "reject",
    },
    // works
    TypeA: {
        label: "string",
        id: "string",
        "result?": "TypeA | TypeX",
        "+": "reject",
    },
    // does not work
    TypeB: {
        label: "string",
        id: "string",
        "result?": "TypeB | TypeX | null",
        "+": "reject",
    },
    // works
    TypeC: {
        label: "string",
        id: "string",
        "result?": "TypeA | TypeX | null",
        "+": "reject",
    },
});
const types = ruleset.export();

const data = {
    label: "hi",
    id: "C",
    result: {label: "A", id: "B"},
}

console.log(types.TypeA(data).summary); // undefined
console.log(types.TypeB(data).summary); // result.label must be removed
console.log(types.TypeC(data).summary); // undefined

TypeA and TypeC handles the input data as I expected. TypeB rejects it - the only difference is that result can now be null.

Activity

  1. added
    bug
    on Jan 29, 2025
  2. ssalbdivad commented on Jan 29, 2025

    @ssalbdivad
    Member

    Looks like this is another JIT-related cyclic compilation bug (see #1188), as you can workaround it by setting jitless: true in your scope config:

    export const ruleset = scope(
    	{
            // ...
    	},
    	{ jitless: true }
    )
  3. added
    confirmed
    on Jan 29, 2025
  4. trsaunders commented on Jan 30, 2025

    @trsaunders
    Author

    Thanks for the tip about jitless that did indeed allow the example in the report to work, but did not help in my more complex real case. After a bit of experimentation I found that jitless does not have any effect if the offending type is referenced by a Record:

    import { scope } from "arktype";
    
    export const mod = scope({
        TypeX: {
            id: "string",
            "+": "reject",
        },
        // works with jitless=true, but not if TypeD references it
        TypeB: {
            label: "string",
            id: "string",
            "result?": "TypeB | TypeX | null",
            "+": "reject",
        },
        // TypeD breaks TypeB when present
        TypeD: { something: "Record<string, TypeB>"},
        // but these do not break it
        TypeE: { something: "TypeB"},
        TypeF: { something: "TypeB[]"},
    }, { jitless: true });
    const types = mod.export();
    
    const data = {
        label: "hi",
        id: "C",
        result: {label: "A", id: "B"},
    }
    
    console.log(types.TypeB(data).summary); // result.label must be removed

    If you comment out TypeD: ... then the result is as expected. The presence of TypeE and TypeF do not seem to cause a problem.

  5. ssalbdivad commented on Jan 30, 2025

    @ssalbdivad
    Member

    @trsaunders For now then I would recommend invoking these recursive references through a narrow instead of allowing the type system to compute them directly.

  6. moved this from To do to Backlog in arktypeioon Jan 30, 2025
  7. trsaunders commented on Feb 10, 2025

    @trsaunders
    Author

    @ssalbdivad thanks for the suggestion about using narrow, I'm only just getting back to this now. Is this the sort of thing you were suggesting (it appears to work), or am I missing a better way? Is there a way to pass through res from the narrow?

    import { scope, type } from "arktype";
    
    export const mod = scope({
        TypeX: {
            id: "string",
            "+": "reject",
        },
        _TypeB: {
            label: "string",
            id: "string",
            "result?": "TypeB | TypeX | null",
            "+": "reject",
        },
        TypeB: () => type("object").narrow((input, ctx) => {
            const res = mod.type("_TypeB")(input);
    
            if (res instanceof type.errors) {
                return false
            }
            return true;
        }),
    });
    const types = mod.export();
    
    const data = {
        label: "hi",
        id: "C",
        result: {label: "A", id: "B"},
    }
    
    console.log(types.TypeB(data).summary);
  8. moved this from Backlog to Implemented in arktypeioon Apr 16, 2025
  9. moved this from Implemented to Done (merged or closed) in arktypeioon Apr 16, 2025
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