Core Utilities

Object Utilities

Utilities for manipulating, transforming, and composing object types in TypeScript. These types allow you to combine, filter, select, modify, and map object properties in a flexible and type-safe way.

Object Utilities

Utilities for manipulating, transforming, and composing object types in TypeScript. These types allow you to combine, filter, select, modify, and map object properties in a flexible and type-safe way.

Installation

npm i -D @halvaradop/ts-utility-types

Usage

import type { , ,  } from "@halvaradop/ts-utility-types/objects"
import type * as  from "@halvaradop/ts-utility-types/objects"


type  = .
  • AppendKeyValue
  • ExcludePrivateKeys
  • FlattenProperties
  • Get
  • GetOptional
  • GetRequired
  • Intersection
  • MapTypes
  • Merge
  • MergeAll
  • Mutable
  • ObjectEntries
  • OmitByType
  • PartialByKeys
  • PickByType
  • Properties
  • ReplaceKeys
  • RequiredByKeys
  • ToPrimitive

Properties<Obj1, Obj2, Common>

Creates a union of the keys of two objects. If Common is true, returns only the common keys.

ArgumentTypeDescription
Obj1objectThe first object type.
Obj2objectThe second object type.
Commonboolean (default false)If true, returns only common keys; all keys if false.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface Foo {
  : string
}

interface Bar {
  : number
}

type PropsFooBar = <Foo, Bar>
type PropsFooBar = "foo" | "bar"
type CommonProps = <Foo & { : boolean }, Bar & { : boolean }, true>
type CommonProps = "shared"

Intersection<Obj1, Obj2>

Creates a new object with the keys that are in one but not the other.

ArgumentTypeDescription
Obj1objectThe first object type.
Obj2objectThe second object type.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface Foo {
  : string
  : string
}

interface Bar {
  : string
  : string
  : number
}

type DiffFoo = <Foo, Bar>
type DiffFoo = {
    gender: number;
}
type DiffBar = <Bar, Foo>
type DiffBar = {
    gender: number;
}

Object Manipulation

These utilities work with both interfaces and type aliases, providing flexible object type transformations.

PickByType<Obj, Type>

Selects the properties of an object that are assignable to a given type.

ArgumentTypeDescription
ObjobjectThe object to pick properties from.
TypeunknownThe type to filter properties by.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface User {
  : string
  : string
  : number
  : boolean
}

type UserStrings = <User, string>
type UserStrings = {
    name: string;
    lastname: string;
}
type UserNumbers = <User, number>
type UserNumbers = {
    age: number;
}
type UserBooleans = <User, boolean>
type UserBooleans = {
    isActive: boolean;
}

PartialByKeys<Obj, Keys>

Makes the specified keys of an object optional (all keys by default).

ArgumentTypeDescription
ObjobjectThe object to make properties optional in.
Keyskeyof Obj (default all keys)The keys to make optional.

Related: RequiredByKeys

import type {  } from "@halvaradop/ts-utility-types/objects"

interface User {
  : string
  : string
  : number
}

type UserPartialName = <User, "name">
type UserPartialName = {
    name?: string | undefined;
    lastname: string;
    age: number;
}
type UserAllPartial = <User>
type UserAllPartial = {
    name?: string | undefined;
    lastname?: string | undefined;
    age?: number | undefined;
}

OmitByType<Obj, Type>

Omits the properties of an object that are assignable to a given type.

ArgumentTypeDescription
ObjobjectThe object to omit properties from.
TypeunknownThe type to filter out properties by.

Related: PickByType

import type {  } from "@halvaradop/ts-utility-types/objects"

interface User {
  : string
  : string
  : number
}

type UserExcludeStrings = <User, string>
type UserExcludeStrings = {
    age: number;
}
type UserExcludeNumbers = <User, number>
type UserExcludeNumbers = {
    name: string;
    lastname: string;
}

FlattenProperties<Obj, Keys>

Extracts the value of a key and merges it into the object (experimental).

ArgumentTypeDescription
ObjobjectThe object to flatten a property from.
Keyskeyof ObjThe key(s) whose value(s) to merge into object.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface User {
  : string
  : string
  : { : string; : string }
}

type UserAddress = <User, "address">
type UserAddress = {
    name: string;
    lastname: string;
    street: string;
    avenue: string;
}

ExcludePrivateKeys<Obj>

Removes properties whose name starts with an underscore (_).

ArgumentTypeDescription
ObjobjectThe object to remove private keys from.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface User {
  : string
  : number
}

type PublicUser = <User>
type PublicUser = {
    public: number;
}

RequiredByKeys<Obj, Keys>

Makes the specified keys of an object required (all keys by default).

ArgumentTypeDescription
ObjobjectThe object to make keys required in.
Keyskeyof ObjThe keys to make required (default all).
import type {  } from "@halvaradop/ts-utility-types/objects"

interface User {
  ?: string
  ?: number
  ?: string
}

type UserRequiredName = <User, "name">
type UserRequiredName = {
    name: string;
    age?: number | undefined;
    address?: string | undefined;
}

Mutable<Obj>

Makes the properties of an object mutable (removes readonly). Affects only the first level.

ArgumentTypeDescription
ObjobjectThe object to make mutable.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface User {
  readonly : string
  readonly : string
  readonly : number
}

type NonReadonlyUser = <User>
type NonReadonlyUser = {
    name: string;
    lastname: string;
    age: number;
}

AppendKeyValue<Obj, Property, Value>

Adds a new property to an object.

ArgumentTypeDescription
ObjobjectThe object to add a property to.
PropertystringThe property name to add.
ValueunknownThe type of the new property.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface User {
  : string
}

type UserAppendLastname = <User, "lastname", string>
type UserAppendLastname = {
    name: string;
    lastname: string;
}

ObjectEntries<Obj>

Returns a union of the [key, value] entries of the object.

ArgumentTypeDescription
ObjobjectThe object to get entries from.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface Foo {
  : string
  : number
  ?: boolean
}

type FooEntries = <Foo>
type FooEntries = ["foo", string] | ["bar", number] | ["foobar", boolean]

ReplaceKeys<Obj, Keys, Replace>

Replaces the types of the specified keys with those defined in Replace.

ArgumentTypeDescription
ObjobjectThe object to replace keys in.
KeysstringThe keys to replace.
ReplaceobjectThe mapping of keys to new types.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface Foo {
  : string
  : number
  : boolean
}

type ReplaceStrings = <Foo, "foo" | "foobar", { : number; : number }>
type ReplaceStrings = {
    foo: number;
    bar: number;
    foobar: number;
}

MapTypes<Obj, Mapper>

Maps the types of the properties that match from in the Mapper, replacing them with to.

ArgumentTypeDescription
ObjobjectThe object to map types in.
MapperobjectThe mapping object with from and to.
import type {  } from "@halvaradop/ts-utility-types/objects"

type ReplaceTypesI = <{ : string; : number }, { : number; : boolean }>
type ReplaceTypesI = {
    foo: string;
    bar: boolean;
}

ToPrimitive<Obj>

ArgumentTypeDescription
ObjobjectThe object to convert to primitives.

Converts the properties of an object to their primitive types.

import type {  } from "@halvaradop/ts-utility-types/objects"

interface User {
  : "Foo"
  : "Bar"
  : 30
}

type UserPrimitive = <User>
type UserPrimitive = {
    name: string;
    lastname: string;
    age: number;
}

Get<Obj1, Obj2, Key>

Returns the value of a key in one of the two objects.

ArgumentTypeDescription
Obj1objectThe first object to check.
Obj2objectThe second object to check.
KeystringThe key to get the value for.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface Foo {
  : string
}

interface Bar {
  : number
}

type FooValue = <Foo, Bar, "foo">
type FooValue = string
type BarValue = <Foo, Bar, "bar">
type BarValue = number

GetRequired<Obj>

Returns only the required keys of an object.

ArgumentTypeDescription
ObjobjectThe object to get required keys from.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface User {
  : string
  ?: number
  ?: string
}

type UserRequired = <User>
type UserRequired = {
    name: string;
}

GetOptional<Obj>

Returns only the optional keys of an object.

ArgumentTypeDescription
ObjobjectThe object to get optional keys from.
import type {  } from "@halvaradop/ts-utility-types/objects"

interface User {
  : string
  ?: number
  ?: string
}

type UserOptional = <User>
type UserOptional = {
    age?: number | undefined;
    address?: string | undefined;
}