Skip to content

Improve partially discriminated error context #1435

Closed

Description

@paulzhng

Report a bug

The error message "locks into" the third union variant and returns error messages based on the assumption the third branch is the correct branch, even though no information is given that could lead to this assumption.

I would expect a union error on the root level instead.

🔎 Search Terms

  • error
  • union
  • no discriminator

🧩 Context

  • ArkType version: 2.1-20
  • TypeScript version (5.1+): 5.7.3

🧑‍💻 Repro

Playground Link: https://arktype.io/playground?code=import%2520%257B%2520type%2520%257D%2520from%2520%2522arktype%2522%253B%250A%250Aconst%2520Thing%2520%253D%2520type.or%28%250A%2509%257B%250A%2509%2509type%253A%2520%2522%27a%27%2522%252C%250A%2509%2509val1%253A%2520%2522number%2522%252C%250A%2509%257D%252C%250A%2509%257B%250A%2509%2509type%253A%2520%2522%27b%27%2522%252C%250A%2509%2509val2%253A%2520%2522number%2522%252C%250A%2509%257D%252C%250A%2509%257B%250A%2509%2509type%253A%2520%2522%27c%27%2520%257C%2520%27d%27%2522%252C%250A%2509%2509val3%253A%2520%2522number%2522%252C%250A%2509%257D%252C%250A%29%253B%250A%250A%252F%252F%2520type%2520must%2520be%2520%2522c%2522%2520or%2520%2522d%2522%2520%28was%2520missing%29%250A%252F%252F%2520val3%2520must%2520be%2520a%2520number%2520%28was%2520missing%29%250Aconst%2520out%2520%253D%2520Thing%28%257B%257D%29%253B%250A

import { type } from "arktype";

const Thing = type.or(
	{
		type: "'a'",
		val1: "number",
	},
	{
		type: "'b'",
		val2: "number",
	},
	{
		type: "'c' | 'd'",
		val3: "number",
	},
);

// type must be "c" or "d" (was missing)
// val3 must be a number (was missing)
const out = Thing({});

Activity

  1. added
    bug
    on Apr 23, 2025
  2. changed the title [-]Errors for unions without clear discriminator are too narrow[/-] [+]Improve partially discriminated error context[/+] on Apr 23, 2025
  3. added
    enhancement
    confirmed
    and removed
    bug
    on Apr 23, 2025
  4. ssalbdivad commented on Apr 23, 2025

    @ssalbdivad
    Member

    This is something that's been on my radar for a while and I've recently been considering some ways to address it and get the best of both worlds from the discrimination logic.

  5. paulzhng commented on Apr 23, 2025

    @paulzhng
    Author

    FYI, I tried it with jitless: true and for JIT disabled, the error message is expected:

    import { configure } from "arktype/config";
    
    configure({
    	jitless: true,
    });
    
    const { type } = await import("arktype");
    
    const Thing = type.or(
    	{
    		type: "'a'",
    		val1: "number",
    	},
    	{
    		type: "'b'",
    		val2: "number",
    	},
    	{
    		type: "'c' | 'd'",
    		val3: "number",
    	},
    );
    
    // type must be "c" or "d", "a" or "b" (was missing)
    const out = Thing({});
  6. ssalbdivad commented on Apr 23, 2025

    @ssalbdivad
    Member

    @paulzhng Yeah, the key is combining the extra context you get here which is helpful with the concision you get from discrimination when you really do want to narrow down the error message to apply to a specific branch, possibly with some explanation of why that branch was chosen.

    The jitless non-discriminated traversal has to give one error per branch no matter what.

  7. moved this from To do to Backlog in arktypeioon Apr 23, 2025
  8. ssalbdivad commented on Nov 18, 2026

    @ssalbdivad
    Member

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

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

confirmedenhancement

Type

No type

Projects

  • Status
    Done (merged or closed)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions