Skip to content

Commit 40c20cf

Browse files
committed
refactor: move util types to it's own file
1 parent 7579895 commit 40c20cf

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/types.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ import type {
145145
} from "./math/math";
146146
import type { NavMesh } from "./math/navigationmesh";
147147
import type { Vec2 } from "./math/Vec2";
148+
import type { Defined, MergeObj } from "./utils/types";
148149

149150
/**
150151
* Context handle that contains every KAPLAY function.
@@ -5835,31 +5836,6 @@ export type Tag = string;
58355836
*/
58365837
export type GameObj<T = any> = GameObjRaw & MergeComps<T>;
58375838

5838-
export type UnionToIntersection<U> = (
5839-
U extends any ? (k: U) => void : never
5840-
) extends (k: infer I) => void ? I
5841-
: never;
5842-
5843-
// What defined does is remove prop: never types for left types clean.
5844-
// This could work for the proccess of remove Comp properties in XXXXComp types
5845-
export type Defined<T> = T extends any
5846-
? Pick<T, { [K in keyof T]-?: T[K] extends undefined ? never : K }[keyof T]>
5847-
: never;
5848-
5849-
/**
5850-
* It obligates to TypeScript to Expand the type.
5851-
*
5852-
* Instead of being `{ id: 1 } | { name: "hi" }`
5853-
* makes
5854-
* It's `{ id: 1, name: "hi" }`
5855-
*
5856-
* https://www.totaltypescript.com/concepts/the-prettify-helper
5857-
*
5858-
* Previously Expand<T>
5859-
*/
5860-
export type Prettify<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;
5861-
export type MergeObj<T> = Prettify<UnionToIntersection<Defined<T>>>;
5862-
58635839
type RemoveCompProps<T> = Defined<
58645840
{
58655841
[K in keyof T]: K extends keyof Comp ? never : T[K];

src/utils/types.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Convert a union type to an intersection type.
3+
*/
4+
export type UnionToIntersection<U> = (
5+
U extends any ? (k: U) => void : never
6+
) extends (k: infer I) => void ? I
7+
: never;
8+
9+
/**
10+
* It removes the properties that are undefined.
11+
*/
12+
export type Defined<T> = T extends any
13+
? Pick<T, { [K in keyof T]-?: T[K] extends undefined ? never : K }[keyof T]>
14+
: never;
15+
16+
/**
17+
* It obligates to TypeScript to Expand the type.
18+
*
19+
* Instead of being `{ id: 1 } | { name: "hi" }`
20+
* makes
21+
* It's `{ id: 1, name: "hi" }`
22+
*
23+
* https://www.totaltypescript.com/concepts/the-prettify-helper
24+
*/
25+
export type Expand<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;
26+
27+
/**
28+
* It merges all the objects into one.
29+
*/
30+
export type MergeObj<T> = Expand<UnionToIntersection<Defined<T>>>;

0 commit comments

Comments
 (0)