types-belt
Version:
A comprehensive collection of TypeScript utility types for building robust and maintainable applications
201 lines • 4.94 kB
TypeScript
/**
* Object utility types for TypeScript
* @module ObjectTypes
*/
/**
* Makes all properties in T optional
* @template T - The type to make optional
* @example
* ```typescript
* interface User {
* id: number;
* name: string;
* email: string;
* }
*
* type PartialUser = Partial<User>;
* // Result: { id?: number; name?: string; email?: string; }
* ```
*/
export type Partial<T> = {
[P in keyof T]?: T[P];
};
/**
* Makes all properties in T required
* @template T - The type to make required
* @example
* ```typescript
* interface User {
* id?: number;
* name?: string;
* email?: string;
* }
*
* type RequiredUser = Required<User>;
* // Result: { id: number; name: string; email: string; }
* ```
*/
export type Required<T> = {
[P in keyof T]-?: T[P];
};
/**
* Makes all properties in T readonly
* @template T - The type to make readonly
* @example
* ```typescript
* interface User {
* id: number;
* name: string;
* email: string;
* }
*
* type ReadonlyUser = Readonly<User>;
* // Result: { readonly id: number; readonly name: string; readonly email: string; }
* ```
*/
export type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
/**
* Picks a set of properties from T
* @template T - The source type
* @template K - The keys to pick
* @example
* ```typescript
* interface User {
* id: number;
* name: string;
* email: string;
* password: string;
* }
*
* type UserPublic = Pick<User, 'id' | 'name' | 'email'>;
* // Result: { id: number; name: string; email: string; }
* ```
*/
export type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
/**
* Omits a set of properties from T
* @template T - The source type
* @template K - The keys to omit
* @example
* ```typescript
* interface User {
* id: number;
* name: string;
* email: string;
* password: string;
* }
*
* type UserWithoutPassword = Omit<User, 'password'>;
* // Result: { id: number; name: string; email: string; }
* ```
*/
export type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
/**
* Creates a type with a set of properties K of type T
* @template K - The keys
* @template T - The type for the properties
* @example
* ```typescript
* type UserRoles = Record<'admin' | 'user' | 'guest', boolean>;
* // Result: { admin: boolean; user: boolean; guest: boolean; }
* ```
*/
export type Record<K extends keyof any, T> = {
[P in K]: T;
};
/**
* Extracts the type of a property from an object type
* @template T - The object type
* @template K - The property key
* @example
* ```typescript
* interface User {
* id: number;
* name: string;
* profile: { age: number; city: string; };
* }
*
* type UserName = ExtractProperty<User, 'name'>; // string
* type UserProfile = ExtractProperty<User, 'profile'>; // { age: number; city: string; }
* ```
*/
export type ExtractProperty<T, K extends keyof T> = T[K];
/**
* Makes specific properties optional in T
* @template T - The source type
* @template K - The keys to make optional
* @example
* ```typescript
* interface User {
* id: number;
* name: string;
* email: string;
* phone?: string;
* }
*
* type UserWithOptionalContact = PartialBy<User, 'email' | 'phone'>;
* // Result: { id: number; name: string; email?: string; phone?: string; }
* ```
*/
export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
/**
* Makes specific properties required in T
* @template T - The source type
* @template K - The keys to make required
* @example
* ```typescript
* interface User {
* id?: number;
* name?: string;
* email?: string;
* phone?: string;
* }
*
* type UserWithRequiredId = RequiredBy<User, 'id'>;
* // Result: { id: number; name?: string; email?: string; phone?: string; }
* ```
*/
export type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
/**
* Creates a type with all properties from T except those in K
* @template T - The source type
* @template K - The keys to exclude
* @example
* ```typescript
* interface User {
* id: number;
* name: string;
* email: string;
* password: string;
* createdAt: Date;
* }
*
* type UserWithoutSensitive = ExcludeKeys<User, 'password' | 'createdAt'>;
* // Result: { id: number; name: string; email: string; }
* ```
*/
export type ExcludeKeys<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
/**
* Creates a type with only the specified keys from T
* @template T - The source type
* @template K - The keys to include
* @example
* ```typescript
* interface User {
* id: number;
* name: string;
* email: string;
* password: string;
* createdAt: Date;
* }
*
* type UserBasic = IncludeKeys<User, 'id' | 'name' | 'email'>;
* // Result: { id: number; name: string; email: string; }
* ```
*/
export type IncludeKeys<T, K extends keyof T> = Pick<T, K>;
//# sourceMappingURL=object.d.ts.map