@colyseus/schema 3.0.74 → 3.0.75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Definition, DefinitionType, PrimitiveType } from "../annotations";
|
|
1
|
+
import type { Definition, DefinitionType, PrimitiveType, RawPrimitiveType } from "../annotations";
|
|
2
2
|
import type { Schema } from "../Schema";
|
|
3
3
|
import type { ArraySchema } from "./custom/ArraySchema";
|
|
4
4
|
import type { CollectionSchema } from "./custom/CollectionSchema";
|
|
@@ -30,21 +30,29 @@ export type InferValueType<T extends DefinitionType> = T extends "string" ? stri
|
|
|
30
30
|
};
|
|
31
31
|
} ? (ChildType extends Record<string | number, string | number> ? CollectionSchema<ChildType[keyof ChildType]> : CollectionSchema<ChildType>) : T extends {
|
|
32
32
|
type: infer ChildType;
|
|
33
|
-
} ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType] : ChildType) : T extends Array<infer ChildType extends Constructor> ? InstanceType<ChildType>[] : T extends Array<infer ChildType> ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType][] : ChildType[]) : T extends {
|
|
33
|
+
} ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType] : ChildType) : T extends Array<infer ChildType extends Constructor> ? InstanceType<ChildType>[] : T extends Array<infer ChildType extends RawPrimitiveType> ? InferValueType<ChildType>[] : T extends Array<infer ChildType> ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType][] : ChildType[]) : T extends {
|
|
34
34
|
array: infer ChildType extends Constructor;
|
|
35
35
|
} ? InstanceType<ChildType>[] : T extends {
|
|
36
|
+
array: infer ChildType extends RawPrimitiveType;
|
|
37
|
+
} ? InferValueType<ChildType>[] : T extends {
|
|
36
38
|
array: infer ChildType;
|
|
37
39
|
} ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType][] : ChildType[]) : T extends {
|
|
38
40
|
map: infer ChildType extends Constructor;
|
|
39
41
|
} ? MapSchema<InstanceType<ChildType>> : T extends {
|
|
42
|
+
map: infer ChildType extends RawPrimitiveType;
|
|
43
|
+
} ? MapSchema<InferValueType<ChildType>> : T extends {
|
|
40
44
|
map: infer ChildType;
|
|
41
45
|
} ? (ChildType extends Record<string | number, string | number> ? MapSchema<ChildType[keyof ChildType]> : MapSchema<ChildType>) : T extends {
|
|
42
46
|
set: infer ChildType extends Constructor;
|
|
43
47
|
} ? SetSchema<InstanceType<ChildType>> : T extends {
|
|
48
|
+
set: infer ChildType extends RawPrimitiveType;
|
|
49
|
+
} ? SetSchema<InferValueType<ChildType>> : T extends {
|
|
44
50
|
set: infer ChildType;
|
|
45
51
|
} ? (ChildType extends Record<string | number, string | number> ? SetSchema<ChildType[keyof ChildType]> : SetSchema<ChildType>) : T extends {
|
|
46
52
|
collection: infer ChildType extends Constructor;
|
|
47
53
|
} ? CollectionSchema<InstanceType<ChildType>> : T extends {
|
|
54
|
+
collection: infer ChildType extends RawPrimitiveType;
|
|
55
|
+
} ? CollectionSchema<InferValueType<ChildType>> : T extends {
|
|
48
56
|
collection: infer ChildType;
|
|
49
57
|
} ? (ChildType extends Record<string | number, string | number> ? CollectionSchema<ChildType[keyof ChildType]> : CollectionSchema<ChildType>) : T extends Constructor ? InstanceType<T> : T extends Record<string | number, string | number> ? T[keyof T] : T extends PrimitiveType ? T : never;
|
|
50
58
|
export type InferSchemaInstanceType<T extends Definition> = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HelperTypes.js","sourceRoot":"","sources":["../../src/types/HelperTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { Definition, DefinitionType, PrimitiveType, RawPrimitiveType } from \"../annotations\";\nimport type { Schema } from \"../Schema\";\nimport type { ArraySchema } from \"./custom/ArraySchema\";\nimport type { CollectionSchema } from \"./custom/CollectionSchema\";\nimport type { MapSchema } from \"./custom/MapSchema\";\nimport type { SetSchema } from \"./custom/SetSchema\";\n\nexport type Constructor<T = {}> = new (...args: any[]) => T;\n\nexport interface Collection<K = any, V = any, IT = V> {\n [Symbol.iterator](): IterableIterator<IT>;\n forEach(callback: Function): void;\n entries(): IterableIterator<[K, V]>;\n}\n\nexport type InferValueType<T extends DefinitionType> =\n T extends \"string\" ? string\n : T extends \"number\" ? number\n : T extends \"int8\" ? number\n : T extends \"uint8\" ? number\n : T extends \"int16\" ? number\n : T extends \"uint16\" ? number\n : T extends \"int32\" ? number\n : T extends \"uint32\" ? number\n : T extends \"int64\" ? number\n : T extends \"uint64\" ? number\n : T extends \"float32\" ? number\n : T extends \"float64\" ? number\n : T extends \"boolean\" ? boolean\n\n // Handle { type: ... } patterns\n : T extends { type: infer ChildType extends PrimitiveType } ? InferValueType<ChildType>\n : T extends { type: infer ChildType extends Constructor } ? InstanceType<ChildType>\n : T extends { type: Array<infer ChildType> } ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType][] : ChildType[]) // TS ENUM\n : T extends { type: { map: infer ChildType } } ? (ChildType extends Record<string | number, string | number> ? MapSchema<ChildType[keyof ChildType]> : MapSchema<ChildType>) // TS ENUM\n : T extends { type: { set: infer ChildType } } ? (ChildType extends Record<string | number, string | number> ? SetSchema<ChildType[keyof ChildType]> : SetSchema<ChildType>) // TS ENUM\n : T extends { type: { collection: infer ChildType } } ? (ChildType extends Record<string | number, string | number> ? CollectionSchema<ChildType[keyof ChildType]> : CollectionSchema<ChildType>) // TS ENUM\n : T extends { type: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType] : ChildType) // TS ENUM\n\n // Handle direct array patterns\n : T extends Array<infer ChildType extends Constructor> ? InstanceType<ChildType>[]\n : T extends Array<infer ChildType> ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType][] : ChildType[]) // TS ENUM\n\n // Handle collection object patterns\n : T extends { array: infer ChildType extends Constructor } ? InstanceType<ChildType>[]\n : T extends { array: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType][] : ChildType[]) // TS ENUM\n\n : T extends { map: infer ChildType extends Constructor } ? MapSchema<InstanceType<ChildType>>\n : T extends { map: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? MapSchema<ChildType[keyof ChildType]> : MapSchema<ChildType>) // TS ENUM\n\n : T extends { set: infer ChildType extends Constructor } ? SetSchema<InstanceType<ChildType>>\n : T extends { set: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? SetSchema<ChildType[keyof ChildType]> : SetSchema<ChildType>) // TS ENUM\n\n : T extends { collection: infer ChildType extends Constructor } ? CollectionSchema<InstanceType<ChildType>>\n : T extends { collection: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? CollectionSchema<ChildType[keyof ChildType]> : CollectionSchema<ChildType>) // TS ENUM\n\n // Handle direct types\n : T extends Constructor ? InstanceType<T>\n : T extends Record<string | number, string | number> ? T[keyof T] // TS ENUM\n : T extends PrimitiveType ? T\n\n : never;\n\nexport type InferSchemaInstanceType<T extends Definition> = {\n [K in keyof T]: T[K] extends (...args: any[]) => any\n ? (T[K] extends new (...args: any[]) => any ? InferValueType<T[K]> : T[K])\n : InferValueType<T[K]>\n} & Schema;\n\nexport type NonFunctionProps<T> = Omit<T, {\n [K in keyof T]: T[K] extends Function ? K : never;\n}[keyof T]>;\n\nexport type NonFunctionPropNames<T> = {\n [K in keyof T]: T[K] extends Function ? never : K\n}[keyof T];\n\nexport type NonFunctionNonPrimitivePropNames<T> = {\n [K in keyof T]: T[K] extends Function\n ? never\n : T[K] extends number | string | boolean\n ? never\n : K\n}[keyof T];\n\n// Helper to recursively convert Schema instances to their JSON representation\ntype ToJSONValue<U> = U extends Schema ? ToJSON<U> : U;\n\nexport type ToJSON<T> = NonFunctionProps<{\n [K in keyof T]:\n T[K] extends MapSchema<infer U> ? Record<string, ToJSONValue<U>>\n : T[K] extends Map<string, infer U> ? Record<string, ToJSONValue<U>>\n : T[K] extends ArraySchema<infer U> ? ToJSONValue<U>[]\n : T[K] extends SetSchema<infer U> ? ToJSONValue<U>[]\n : T[K] extends CollectionSchema<infer U> ? ToJSONValue<U>[]\n : T[K] extends Schema ? ToJSON<T[K]>\n : T[K]\n}>;\n\n// Helper type to check if T is exactly 'never' (meaning no InitProps was provided)\nexport type IsNever<T> = [T] extends [never] ? true : false;\n\n/**\n * Type helper for .assign() method - allows assigning values in a flexible way\n * - Primitives can be assigned directly\n * - Schema instances can be assigned from plain objects or Schema instances\n * - Collections can be assigned from their JSON representations\n */\nexport type AssignableProps<T> = {\n [K in NonFunctionPropNames<T>]?: T[K] extends MapSchema<infer U>\n ? MapSchema<U> | Record<string, U extends Schema ? (U | AssignableProps<U>) : U>\n : T[K] extends ArraySchema<infer U>\n ? ArraySchema<U> | (U extends Schema ? (U | AssignableProps<U>)[] : U[])\n : T[K] extends SetSchema<infer U>\n ? SetSchema<U> | Set<U> | (U extends Schema ? (U | AssignableProps<U>)[] : U[])\n : T[K] extends CollectionSchema<infer U>\n ? CollectionSchema<U> | (U extends Schema ? (U | AssignableProps<U>)[] : U[])\n : T[K] extends Schema\n ? T[K] | AssignableProps<T[K]>\n : T[K]\n};"]}
|
|
1
|
+
{"version":3,"file":"HelperTypes.js","sourceRoot":"","sources":["../../src/types/HelperTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { Definition, DefinitionType, PrimitiveType, RawPrimitiveType } from \"../annotations\";\nimport type { Schema } from \"../Schema\";\nimport type { ArraySchema } from \"./custom/ArraySchema\";\nimport type { CollectionSchema } from \"./custom/CollectionSchema\";\nimport type { MapSchema } from \"./custom/MapSchema\";\nimport type { SetSchema } from \"./custom/SetSchema\";\n\nexport type Constructor<T = {}> = new (...args: any[]) => T;\n\nexport interface Collection<K = any, V = any, IT = V> {\n [Symbol.iterator](): IterableIterator<IT>;\n forEach(callback: Function): void;\n entries(): IterableIterator<[K, V]>;\n}\n\nexport type InferValueType<T extends DefinitionType> =\n T extends \"string\" ? string\n : T extends \"number\" ? number\n : T extends \"int8\" ? number\n : T extends \"uint8\" ? number\n : T extends \"int16\" ? number\n : T extends \"uint16\" ? number\n : T extends \"int32\" ? number\n : T extends \"uint32\" ? number\n : T extends \"int64\" ? number\n : T extends \"uint64\" ? number\n : T extends \"float32\" ? number\n : T extends \"float64\" ? number\n : T extends \"boolean\" ? boolean\n\n // Handle { type: ... } patterns\n : T extends { type: infer ChildType extends PrimitiveType } ? InferValueType<ChildType>\n : T extends { type: infer ChildType extends Constructor } ? InstanceType<ChildType>\n : T extends { type: Array<infer ChildType> } ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType][] : ChildType[]) // TS ENUM\n : T extends { type: { map: infer ChildType } } ? (ChildType extends Record<string | number, string | number> ? MapSchema<ChildType[keyof ChildType]> : MapSchema<ChildType>) // TS ENUM\n : T extends { type: { set: infer ChildType } } ? (ChildType extends Record<string | number, string | number> ? SetSchema<ChildType[keyof ChildType]> : SetSchema<ChildType>) // TS ENUM\n : T extends { type: { collection: infer ChildType } } ? (ChildType extends Record<string | number, string | number> ? CollectionSchema<ChildType[keyof ChildType]> : CollectionSchema<ChildType>) // TS ENUM\n : T extends { type: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType] : ChildType) // TS ENUM\n\n // Handle direct array patterns\n : T extends Array<infer ChildType extends Constructor> ? InstanceType<ChildType>[]\n : T extends Array<infer ChildType extends RawPrimitiveType> ? InferValueType<ChildType>[] // primitive types\n : T extends Array<infer ChildType> ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType][] : ChildType[]) // TS ENUM\n\n // Handle collection object patterns\n : T extends { array: infer ChildType extends Constructor } ? InstanceType<ChildType>[]\n : T extends { array: infer ChildType extends RawPrimitiveType } ? InferValueType<ChildType>[] // primitive types\n : T extends { array: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType][] : ChildType[]) // TS ENUM\n\n : T extends { map: infer ChildType extends Constructor } ? MapSchema<InstanceType<ChildType>>\n : T extends { map: infer ChildType extends RawPrimitiveType } ? MapSchema<InferValueType<ChildType>> // primitive types\n : T extends { map: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? MapSchema<ChildType[keyof ChildType]> : MapSchema<ChildType>) // TS ENUM\n\n : T extends { set: infer ChildType extends Constructor } ? SetSchema<InstanceType<ChildType>>\n : T extends { set: infer ChildType extends RawPrimitiveType } ? SetSchema<InferValueType<ChildType>> // primitive types\n : T extends { set: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? SetSchema<ChildType[keyof ChildType]> : SetSchema<ChildType>) // TS ENUM\n\n : T extends { collection: infer ChildType extends Constructor } ? CollectionSchema<InstanceType<ChildType>>\n : T extends { collection: infer ChildType extends RawPrimitiveType } ? CollectionSchema<InferValueType<ChildType>> // primitive types\n : T extends { collection: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? CollectionSchema<ChildType[keyof ChildType]> : CollectionSchema<ChildType>) // TS ENUM\n\n // Handle direct types\n : T extends Constructor ? InstanceType<T>\n : T extends Record<string | number, string | number> ? T[keyof T] // TS ENUM\n : T extends PrimitiveType ? T\n\n : never;\n\nexport type InferSchemaInstanceType<T extends Definition> = {\n [K in keyof T]: T[K] extends (...args: any[]) => any\n ? (T[K] extends new (...args: any[]) => any ? InferValueType<T[K]> : T[K])\n : InferValueType<T[K]>\n} & Schema;\n\nexport type NonFunctionProps<T> = Omit<T, {\n [K in keyof T]: T[K] extends Function ? K : never;\n}[keyof T]>;\n\nexport type NonFunctionPropNames<T> = {\n [K in keyof T]: T[K] extends Function ? never : K\n}[keyof T];\n\nexport type NonFunctionNonPrimitivePropNames<T> = {\n [K in keyof T]: T[K] extends Function\n ? never\n : T[K] extends number | string | boolean\n ? never\n : K\n}[keyof T];\n\n// Helper to recursively convert Schema instances to their JSON representation\ntype ToJSONValue<U> = U extends Schema ? ToJSON<U> : U;\n\nexport type ToJSON<T> = NonFunctionProps<{\n [K in keyof T]:\n T[K] extends MapSchema<infer U> ? Record<string, ToJSONValue<U>>\n : T[K] extends Map<string, infer U> ? Record<string, ToJSONValue<U>>\n : T[K] extends ArraySchema<infer U> ? ToJSONValue<U>[]\n : T[K] extends SetSchema<infer U> ? ToJSONValue<U>[]\n : T[K] extends CollectionSchema<infer U> ? ToJSONValue<U>[]\n : T[K] extends Schema ? ToJSON<T[K]>\n : T[K]\n}>;\n\n// Helper type to check if T is exactly 'never' (meaning no InitProps was provided)\nexport type IsNever<T> = [T] extends [never] ? true : false;\n\n/**\n * Type helper for .assign() method - allows assigning values in a flexible way\n * - Primitives can be assigned directly\n * - Schema instances can be assigned from plain objects or Schema instances\n * - Collections can be assigned from their JSON representations\n */\nexport type AssignableProps<T> = {\n [K in NonFunctionPropNames<T>]?: T[K] extends MapSchema<infer U>\n ? MapSchema<U> | Record<string, U extends Schema ? (U | AssignableProps<U>) : U>\n : T[K] extends ArraySchema<infer U>\n ? ArraySchema<U> | (U extends Schema ? (U | AssignableProps<U>)[] : U[])\n : T[K] extends SetSchema<infer U>\n ? SetSchema<U> | Set<U> | (U extends Schema ? (U | AssignableProps<U>)[] : U[])\n : T[K] extends CollectionSchema<infer U>\n ? CollectionSchema<U> | (U extends Schema ? (U | AssignableProps<U>)[] : U[])\n : T[K] extends Schema\n ? T[K] | AssignableProps<T[K]>\n : T[K]\n};"]}
|
package/package.json
CHANGED
package/src/types/HelperTypes.ts
CHANGED
|
@@ -39,19 +39,24 @@ export type InferValueType<T extends DefinitionType> =
|
|
|
39
39
|
|
|
40
40
|
// Handle direct array patterns
|
|
41
41
|
: T extends Array<infer ChildType extends Constructor> ? InstanceType<ChildType>[]
|
|
42
|
+
: T extends Array<infer ChildType extends RawPrimitiveType> ? InferValueType<ChildType>[] // primitive types
|
|
42
43
|
: T extends Array<infer ChildType> ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType][] : ChildType[]) // TS ENUM
|
|
43
44
|
|
|
44
45
|
// Handle collection object patterns
|
|
45
46
|
: T extends { array: infer ChildType extends Constructor } ? InstanceType<ChildType>[]
|
|
47
|
+
: T extends { array: infer ChildType extends RawPrimitiveType } ? InferValueType<ChildType>[] // primitive types
|
|
46
48
|
: T extends { array: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? ChildType[keyof ChildType][] : ChildType[]) // TS ENUM
|
|
47
49
|
|
|
48
50
|
: T extends { map: infer ChildType extends Constructor } ? MapSchema<InstanceType<ChildType>>
|
|
51
|
+
: T extends { map: infer ChildType extends RawPrimitiveType } ? MapSchema<InferValueType<ChildType>> // primitive types
|
|
49
52
|
: T extends { map: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? MapSchema<ChildType[keyof ChildType]> : MapSchema<ChildType>) // TS ENUM
|
|
50
53
|
|
|
51
54
|
: T extends { set: infer ChildType extends Constructor } ? SetSchema<InstanceType<ChildType>>
|
|
55
|
+
: T extends { set: infer ChildType extends RawPrimitiveType } ? SetSchema<InferValueType<ChildType>> // primitive types
|
|
52
56
|
: T extends { set: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? SetSchema<ChildType[keyof ChildType]> : SetSchema<ChildType>) // TS ENUM
|
|
53
57
|
|
|
54
58
|
: T extends { collection: infer ChildType extends Constructor } ? CollectionSchema<InstanceType<ChildType>>
|
|
59
|
+
: T extends { collection: infer ChildType extends RawPrimitiveType } ? CollectionSchema<InferValueType<ChildType>> // primitive types
|
|
55
60
|
: T extends { collection: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? CollectionSchema<ChildType[keyof ChildType]> : CollectionSchema<ChildType>) // TS ENUM
|
|
56
61
|
|
|
57
62
|
// Handle direct types
|