typia icon indicating copy to clipboard operation
typia copied to clipboard

Redundant checks on unions

Open a-non-a-mouse opened this issue 4 months ago • 1 comments

📝 Summary

Write a short summary of the bug in here.

  • Typia Version: 9.7.2
  • Expected behavior: Concise checks
  • Actual behavior: Redundant checks

Unions result in redundant checks, effectively if(type === 'A') { if (type === 'A' && ...) { ... } }

⏯ Playground Link

playground

💻 Code occuring the bug

import typia, { tags } from "typia";

type Orange = { name: 'Orange', color: 'orange' };
type Apple = { name: 'Apple', color: 'red' | 'green' };
type Banana = { name: 'Banana', color: 'yellow' };

type Fruit = Orange | Apple | Banana;

declare const fruit: Fruit;
typia.is<Fruit>(fruit);

generates:

...
  const _io2 = (input) => "Banana" === input.name && "yellow" === input.color;
  const _iu0 = (input) =>
    (() => {
      if ("Banana" === input.name) return _io2(input);
...

a-non-a-mouse avatar Oct 10 '25 12:10 a-non-a-mouse

To fix this problem, I have to separate validator functions not to share common logics (is, assert and validate).

Know that redundant code occured, but did not touch it due to typia is much faster than competitors even with the redundant codes. Also, when I individually develop the three validator functions, there can be a mistake that damaging on the typia' core validator functions.

It should be fixed someday, but no plan to do it right now.

samchon avatar Nov 07 '25 13:11 samchon