Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 9, 2026

Discriminated unions often only need shallow validation of a discriminator field, but typia.is<T>() validates the entire object tree. This generates excessive code and wastes runtime for what should be a simple check.

Changes

New APIs

  • typia.isShallow<T>(input, maxDepth?) - Type guard with depth limit (default: 2)
  • typia.createIsShallow<T>(maxDepth?) - Factory for reusable shallow validators

Implementation

  • Depth tracking in IExplore - Added depth and maxDepth fields to track validation depth
  • Early return in CheckerProgrammer.decode() - Returns true when depth >= maxDepth, skipping deep validation
  • incrementDepth() helper - Increments depth when entering nested structures (objects, arrays, tuples, maps, sets)
  • Transformers - Extract maxDepth from runtime arguments, pass to programmer config

Example

type Response = 
  | { status: "success"; data: { /* deeply nested */ } }
  | { status: "error"; message: string };

// Depth 1: only validates discriminator field
if (typia.isShallow<Response>(input, 1)) {
  // Fast type narrowing
}

// Full validation when needed
if (typia.is<Response>(input)) {
  // All fields validated
}

Depth levels:

  • 1: Top-level properties only (discriminator checks)
  • 2: Default, one level of nesting
  • 3+: Progressively deeper validation
  • undefined: Full validation (same as typia.is)
Original prompt

This section details on the original issue you should resolve

<issue_title>Feature: Shallow .is checks or any "cheaper" discriminator check</issue_title>
<issue_description>There are two common use cases for .is: validation and discrimination.

Typia is often overkill for discrimination due to the amount of code it can generate for something like a JSON Schema type. For discrimination, we often don't need to validate the entire object. If there is a discriminator key, for instance, often all standard TS does is check the value of that key.

Ideally, typia could define a minimal type check to have a discriminator check based on the possible incoming types, but as I think the complexity of that is likely too high, I'm suggesting a MaxDepth param on .is or similar:

typia.is<T, N>

// or a new method to avoid over confidence

typia.isLikely<T, N=2> // 2 seems like a good default
typia.matchesToDepth<T, N=2>
```</issue_description>

<agent_instructions>Transform almost same code like `typia.is<T>()`, but limit the iteration depth.

You can do it by referencing both `CheckerProgrammer` and `IsPrrogrammer`.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 9, 2026 13:47
Co-authored-by: samchon <13158709+samchon@users.noreply.github.com>
Co-authored-by: samchon <13158709+samchon@users.noreply.github.com>
Copilot AI changed the title [WIP] Add shallow .is checks for cheaper discrimination Add depth-limited type checking for discriminated unions Jan 9, 2026
Copilot AI requested a review from samchon January 9, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Shallow .is checks or any "cheaper" discriminator check

2 participants