# Utility Types This module provides a collection of TypeScript utility types for common type transformations and class definitions. ## Features - Type-safe property picking and partialization - Schema type definitions - Class definition and hierarchy types - Readonly property handling ## Usage ```typescript import { PartialPick, Schema, PartialSchema, ClassDefinition, ClassHierarchy } from "n-util"; // Example interfaces interface User { id: string; name: string; age: number; email: string; } // Example class class Person { constructor(public name: string) {} } // Using utility types type UserPartial = PartialPick; type UserSchema = Schema; type UserPartialSchema = PartialSchema; type PersonClass = ClassDefinition; type PersonHierarchy = ClassHierarchy; ``` ## API Reference ### PartialPick ```typescript type PartialPick ``` Creates a type with a subset of properties from T, where the specified keys are made optional. - Type Parameters: - `T`: The base type - `K`: Union of keys from T to make optional - Returns: A type with the specified properties from T made optional Example: ```typescript interface User { id: string; name: string; age: number; } type UserPartial = PartialPick; // Equivalent to: // { // name?: string; // age?: number; // } ``` ### Schema ```typescript type Schema ``` Creates a type with a subset of properties from T, removing readonly modifiers. - Type Parameters: - `T`: The base type - `K`: Union of keys from T to include - Returns: A type with the specified properties from T, without readonly modifiers Example: ```typescript interface User { readonly id: string; readonly name: string; age: number; } type UserSchema = Schema; // Equivalent to: // { // id: string; // name: string; // } ``` ### PartialSchema ```typescript type PartialSchema ``` Creates a type with a subset of properties from T, removing readonly modifiers and making them optional. - Type Parameters: - `T`: The base type - `K`: Union of keys from T to include - Returns: A type with the specified properties from T, without readonly modifiers and made optional Example: ```typescript interface User { readonly id: string; readonly name: string; age: number; } type UserPartialSchema = PartialSchema; // Equivalent to: // { // id?: string; // name?: string; // } ``` ### ClassDefinition ```typescript type ClassDefinition ``` Represents a class constructor type for a given class type T. - Type Parameters: - `T`: The class type - Returns: A constructor type that can create instances of T Example: ```typescript class Person { constructor(public name: string) {} } type PersonClass = ClassDefinition; // Equivalent to: // new (...args: any[]) => Person ``` ### ClassHierarchy ```typescript type ClassHierarchy ``` Represents a class type with its prototype set to T. - Type Parameters: - `T`: The class type - Returns: A function type with its prototype set to T Example: ```typescript class Person { constructor(public name: string) {} } type PersonHierarchy = ClassHierarchy; // Equivalent to: // Function & { prototype: Person; } ``` ## Best Practices 1. Use `PartialPick` when you need to make specific properties optional 2. Use `Schema` when you need to work with mutable versions of readonly properties 3. Use `PartialSchema` when you need both optional and mutable properties 4. Use `ClassDefinition` for type-safe class constructors 5. Use `ClassHierarchy` when working with class inheritance and prototypes ## Examples ### Creating Partial Types ```typescript interface User { id: string; name: string; age: number; email: string; } // Make name and age optional type UserUpdate = PartialPick; const update: UserUpdate = { name: "John" // age is optional }; // Make all properties optional and mutable type UserCreate = PartialSchema; const newUser: UserCreate = { name: "John", age: 30 // id and email are optional }; ``` ### Working with Classes ```typescript class Animal { constructor(public name: string) {} } class Dog extends Animal { constructor(name: string, public breed: string) { super(name); } } // Type-safe class constructor type AnimalClass = ClassDefinition; const createAnimal: AnimalClass = Animal; // Working with class hierarchy type AnimalHierarchy = ClassHierarchy; function isAnimal(obj: any): obj is AnimalHierarchy { return obj.prototype instanceof Animal; } ```