Skip to content

Commit

Permalink
Merge pull request #1174 from samchon/features/intersection
Browse files Browse the repository at this point in the history
Fix #1166: nonsensible intersection type case
  • Loading branch information
samchon authored Jul 21, 2024
2 parents b8f796e + 14e2be3 commit 8cf0fed
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@
"suppress-warnings": "^1.0.2",
"tstl": "^3.0.0",
"uuid": "^9.0.1",
"typia": "../typia-6.5.3.tgz"
"typia": "../typia-6.5.4.tgz"
}
}
6 changes: 6 additions & 0 deletions debug/features/intersection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import typia from "typia";

type Art = `${string}art${string}`;
type Bar = `${string}bar${string}`;

console.log(typia.createIs<Art & Bar>().toString());
2 changes: 1 addition & 1 deletion debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"typescript": "^5.4.2"
},
"dependencies": {
"typia": "../typia-6.5.3-dev.20240720.tgz"
"typia": "../typia-6.5.4-dev.20240721.tgz"
}
}
2 changes: 1 addition & 1 deletion errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"typescript": "^5.3.2"
},
"dependencies": {
"typia": "../typia-6.5.3.tgz"
"typia": "../typia-6.5.4.tgz"
}
}
6 changes: 6 additions & 0 deletions errors/src/nonsensible/error_nonsensible_intersection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import typia from "typia";

type Art = `${string}art${string}`;
type Bar = `${string}bar${string}`;

typia.createIs<Art & Bar>();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "6.5.3",
"version": "6.5.4",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "6.5.3",
"version": "6.5.4",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -63,7 +63,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "6.5.3"
"typia": "6.5.4"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.6.0"
Expand Down
24 changes: 14 additions & 10 deletions src/factories/internal/metadata/iterate_metadata_intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const iterate_metadata_intersection =
explore: MetadataFactory.IExplore,
): boolean => {
if (!type.isIntersection()) return false;
else if (
if (
// ONLY OBJECT TYPED INTERSECTION
type.types.every(
(child) =>
(child.getFlags() & ts.TypeFlags.Object) !== 0 &&
Expand Down Expand Up @@ -68,9 +69,18 @@ export const iterate_metadata_intersection =
);
return true;
} else if (children.every((c) => c.objects.length === c.size()))
// ONLY OBJECT TYPED INTERSECTION (DETAILED)
return false;

// VALIDATE EACH TYPES
const nonsensible = () => {
errors.push({
name: children.map((c) => c.getName()).join(" & "),
explore: { ...explore },
messages: ["nonsensible intersection"],
});
return true;
};
const individuals: (readonly [Metadata, number])[] = children
.map((child, i) => [child, i] as const)
.filter(
Expand All @@ -82,7 +92,7 @@ export const iterate_metadata_intersection =
c.arrays.length === 1)) ||
c.templates.length === 1,
);
if (individuals.length !== 1) return false;
if (individuals.length !== 1) return nonsensible();

const objects: Metadata[] = children.filter(
(c) =>
Expand All @@ -109,14 +119,8 @@ export const iterate_metadata_intersection =
if (
atomics.size + constants.length + arrays.size + templates.length > 1 ||
individuals.length + objects.length !== children.length
) {
errors.push({
name: children.map((c) => c.getName()).join(" & "),
explore: { ...explore },
messages: ["nonsensible intersection"],
});
return true;
}
)
return nonsensible();

// RE-GENERATE TYPE
const target: "boolean" | "bigint" | "number" | "string" | "array" =
Expand Down
2 changes: 1 addition & 1 deletion test-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
"typescript": "^5.4.5"
},
"dependencies": {
"typia": "../typia-6.5.3.tgz"
"typia": "../typia-6.5.4.tgz"
}
}
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
"suppress-warnings": "^1.0.2",
"tstl": "^3.0.0",
"uuid": "^9.0.1",
"typia": "../typia-6.5.3.tgz"
"typia": "../typia-6.5.4.tgz"
}
}
2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"tgrid": "^1.0.2",
"tstl": "^3.0.0",
"typescript": "^5.5.3",
"typia": "^6.5.3"
"typia": "^6.5.4"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
Expand Down

0 comments on commit 8cf0fed

Please sign in to comment.