typia
typia copied to clipboard
Redundant checks on unions
📝 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
💻 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);
...
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.