@rustable/utils
Advanced tools
Comparing version 0.3.14 to 0.3.15
@@ -6,3 +6,46 @@ import { Constructor } from './common'; | ||
} | ||
export declare function Type<T extends MaybeGenericConstructor>(target: T, genericParams?: any[]): T; | ||
/** | ||
* Creates a type constructor with generic type parameters. | ||
* This function is used to create type-safe constructors that maintain generic type information at runtime. | ||
* | ||
* @template T - The constructor type that may include generic parameters | ||
* @param target - The target constructor to create a generic type from | ||
* @param genericParams - Optional array of type parameters to apply to the generic type | ||
* @param newWithTypes - Optional flag to indicate if the constructor should receive type parameters (default: false) | ||
* @returns A new constructor with the specified generic type parameters | ||
* | ||
* @example | ||
* ```typescript | ||
* // Define a generic class | ||
* class Container<T> { | ||
* constructor(public value: T) {} | ||
* } | ||
* | ||
* // Create specific type constructors | ||
* const StringContainer = Type(Container, [String]); | ||
* const NumberContainer = Type(Container, [Number]); | ||
* | ||
* // Type-safe instantiation | ||
* const strContainer = new StringContainer("hello"); // OK | ||
* const numContainer = new NumberContainer(42); // OK | ||
* const error = new StringContainer(123); // Type Error | ||
* ``` | ||
* | ||
* @example | ||
* ```typescript | ||
* // Using newWithTypes flag | ||
* class TypedMap<K, V> { | ||
* constructor(keyType: Constructor<K>, valueType: Constructor<V>) { | ||
* // Initialize with type information | ||
* } | ||
* } | ||
* | ||
* const StringNumberMap = Type(TypedMap, [String, Number], true); | ||
* // Constructor will receive [String, Number] as first arguments | ||
* new StringNumberMap(); | ||
* ``` | ||
* | ||
* @throws {Error} When generic parameters are specified for a non-generic type | ||
*/ | ||
export declare function Type<T extends MaybeGenericConstructor>(target: T, genericParams?: any[], newWithTypes?: boolean): T; | ||
export declare function isGenericType(target: any): boolean; | ||
@@ -9,0 +52,0 @@ /** |
@@ -16,3 +16,3 @@ "use strict"; | ||
function Type(target, genericParams) { | ||
function Type(target, genericParams, newWithTypes = false) { | ||
if (target[genericType]) { | ||
@@ -39,3 +39,7 @@ if (!genericParams || genericParams.length === 0) { | ||
constructor(...args) { | ||
super(...args); | ||
if (newWithTypes) { | ||
super(genericParams, ...args); | ||
} else { | ||
super(...args); | ||
} | ||
} | ||
@@ -42,0 +46,0 @@ }; |
{ | ||
"name": "@rustable/utils", | ||
"version": "0.3.14", | ||
"version": "0.3.15", | ||
"description": "Utility TypeScript utilities inspired by Rust, providing type-safe implementations of HashMap, TypeId, deep cloning, hashing, and equality comparison", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
50023
1283