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

Array of unions incorrect assertion #885

Closed
dinode-bk opened this issue Nov 27, 2023 · 10 comments
Closed

Array of unions incorrect assertion #885

dinode-bk opened this issue Nov 27, 2023 · 10 comments
Assignees
Labels
bug Something isn't working good first issue Good for newcomers hacktoberfest help wanted Extra attention is needed invalid This doesn't seem right

Comments

@dinode-bk
Copy link

dinode-bk commented Nov 27, 2023

Bug Report

📝 Summary

Write a short summary of the bug in here.

  • Typia Version: 5.3.0-dev.20231127-2
  • Expected behavior: Error is returned for the invalid value/type of a nested parameter in the array of types union
  • Actual behavior: Success

Array of union types doesn't get validated properly. As soon as array is wrapped around a singular type (rather than a union of types) - validation works as expected. Even though TypeScript highlights the issue when the data is constructed manually, it would be great to validate the data during the runtime and get the same error (for cases when data is received via an API).

⏯ Playground Link

https://typia.io/playground/?script=JYWwDg9gTgLgBDAnmYBDOAzKERwERIqp4DcAUGQKYAeksclAdgK64DqAKspXAN5kBIAIJwAvPiF4ANIIBCY-LOmCAwgrwrlAXwo068QjzYAeDg2owmAEwDOcTtwB8C-gOBWAXHBswowRgDm5AKGXhzkWnAAZHAAFEIAohZwAD5wsknwaSqZAJTkVLTQBtxwicnirqH2XGCUAHRCwahergIAxgAWwAA2VlBMXkJQUKiIxoICAiYOdfWyjpMCaTO1DSqLU47BWhEFesUIpRkVfILVsw2ywQBGrbtkD4X6R3VwOadV3F6X9SrB7XujwKz0O7QgjB8cCsqBgLXsxl+QmclUE7i8eAAjMoQt8atxGjIBPC2l1ev1BnAANpLNpTaqYolTNyefAAJhxUy0TIAukTucCKODIfABnZxIQ0PUAG6oHruWGURGItaNRyOWIwuH5MjCmwQHoNHoQAKxMX5IA

💻 Code occuring the bug

import typia from "typia";

export enum WType {
	A = "A",
	B = "B",
	C = "C",
}

export type W<T extends WType> = {
	id: string;
	type: T;
} & (AExt | BExt | CExt);

export type AExt = {
	type: WType.A;
	a: {
		children: Array<
			W<WType.B>
			| W<WType.C>
		>;
	};
};

export type BExt = {
	type: WType.B;
	b: {};
};

export type CExt = {
	type: WType.C;
	c: {}
};


export const data: W<WType.A> = {
	id: "1",
	type: WType.A,
	a: {
		children: [
			{
				type: 1, // highlighted as a Typescript error.
				id: "2",
			},
		],
	},
};

const res = typia.validate<W<WType.A>>(data);
console.log(res); // prints success.

The expectation for the $input.a.children[0].type to be reported as of the invalid type (same as typescript does at compilation time). Currently the success is returned. Interestingly if WTypeExt.a.children type is changed to Array<W<WType.B>> (i.e. removing the union) the validation works as expected.

@samchon
Copy link
Owner

samchon commented Nov 27, 2023

Hard to understand the type. Can you reproduce it with much simpler type?

@samchon samchon self-assigned this Nov 27, 2023
@samchon samchon added the bug Something isn't working label Nov 27, 2023
@samchon
Copy link
Owner

samchon commented Nov 27, 2023

The combination of union and intersection type comes as never type at TypeScript compiler API now.

Need additional investigation.

@dinode-bk
Copy link
Author

@samchon thank you! I will also try to dig through Typia code on my side

@samchon
Copy link
Owner

samchon commented Nov 27, 2023

It is because type property is duplicated in both W and AExt | BExt | CExt types.

When such duplicated property exists in the intersection type, TypeScript compiler API considers it as never type. I know it is a typical bug, but not possible to fix it by myself. I'll report to TypeScript repo with more investigations, but cannot sure when the TypeScript team fixes it. Until that, I think you should be careful in that case.

import typia from "typia";

export namespace WType {
	export type A = "A";
	export type B = "B";
	export type C = "C";
}

export type W = {
	id: string;
	type: T;
} & (AExt | BExt | CExt);

export type AExt = {
	a: {
		children: Array<
			W<WType.B>
			| W<WType.C>
		>;
	};
};

export type BExt = {
	b: {};
};

export type CExt = {
	c: {}
};


export const data: W<WType.A> = {
	id: "1",
	type: "A",
	a: {
		children: [
			{
				type: 1,
				id: "2",
			} as any,
		],
	},
};

const res = typia.validate<W<WType.A>>(data);
console.log(res); // prints failed.

@samchon samchon added help wanted Extra attention is needed good first issue Good for newcomers hacktoberfest invalid This doesn't seem right labels Nov 27, 2023
@dinode-bk
Copy link
Author

dinode-bk commented Nov 27, 2023

@samchon thank you for such a quick turnaround!
Very weird because Typescript itself picks it up correctly. Didn't expect for the compiler API to be so different

samchon added a commit that referenced this issue Nov 27, 2023
samchon added a commit that referenced this issue Nov 27, 2023
@samchon
Copy link
Owner

samchon commented Nov 27, 2023

Will you try this type? @GoogleFeud

@GoogleFeud
Copy link

GoogleFeud commented Dec 17, 2023

Will you try this type? @GoogleFeud

I think that the W type here is the culprit. They are trying to make W a discriminated union but this isn't really the way to do it. Here is a version that works in both ts-runtime-checks and typia.

@samchon samchon closed this as completed Jan 17, 2024
@dinode-bk
Copy link
Author

dinode-bk commented Jan 18, 2024

@samchon not sure why this issue was closed given there is no resolution.

@GoogleFeud Subjectively it may seem like a culprit to you, however we have a strong use-case to construct types this way and Typescript allows us to do it so. If you have issues with supporting this case in your library, please let us know - we will be happy to assist.

@samchon samchon reopened this Jan 19, 2024
@samchon
Copy link
Owner

samchon commented Jan 19, 2024

Ah, I'm sorry but mis-closed with wrong flag.

This issue is not fixable, so that close as not planned.

@samchon samchon closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers hacktoberfest help wanted Extra attention is needed invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants