Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected assertion fail for union type with optional fields #884

Closed
nahn20 opened this issue Nov 26, 2023 · 1 comment
Closed

Unexpected assertion fail for union type with optional fields #884

nahn20 opened this issue Nov 26, 2023 · 1 comment
Assignees
Labels
bug Something isn't working
Projects

Comments

@nahn20
Copy link

nahn20 commented Nov 26, 2023

📝 Summary

Type equality (assert, is, etc.) with certain union objects with optional fields is parsed as non-equal.

  • Typia Version: 5.3.0-dev.20231127-2
  • Expected behavior: Typia should select the correct object for matching
  • Actual behavior: Typia decides the object does not match the intersection type.
interface A {
	name?: "A";
	someBool: true;
}
interface B {
	name?: "B";
	someBool: boolean | undefined;
}

It seems that Typia only validates that the body matches A and doesn't check whether it matches B. This problem resolves itself if someBool is required as non-undefined, or if name is required.

I believe that A | B (see below) should match given that TypeScript doesn't have any issues with the following:

const body = {
	name: "B",
	someBool: true,
} satisfies A | B;

Playground Link

💻 Code occuring the bug

import typia from "typia";

interface A {
	name?: "A";
	someBool: true;
}
interface A_NonOptional {
	name: "A";
	someBool: true;
}
interface B {
	name?: "B";
	someBool: boolean | undefined;
}
interface C {
	name?: "C";
	someBool: boolean;
}

// Throws (Incorrect; should match B)
const A_and_B = typia.assert<A | B>({
	name: "B",
	someBool: true,
});

// Doesn't throw (Correct)
const A_NonOptional_and_B = typia.assert<A_NonOptional | B>({
	name: "B",
	someBool: true,
});

// Doesn't throw (Correct)
const A_and_C = typia.assert<A | C>({
	name: "C",
	someBool: true,
});
@samchon samchon self-assigned this Nov 27, 2023
@samchon samchon added the bug Something isn't working label Nov 27, 2023
samchon added a commit that referenced this issue Nov 27, 2023
When there's same type exists in both `atomics` and `constants` and try to predicate union type about that case, `typia` mis-computes whether each types are intersected or not.

This PR fixes the bug, just by adding the special logic about the same typed `atomics` and `constants` are compatible.
@samchon
Copy link
Owner

samchon commented Nov 27, 2023

Succeeded to fix this bug.

Will try to fix #885 bug, but if needs lots of time, then will publish immediately.

Thanks for reporting.

@samchon samchon added this to To do in V5 Update via automation Nov 27, 2023
V5 Update automation moved this from To do to Done Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Development

No branches or pull requests

2 participants