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.
import type * as Objects from "@halvaradop/ts-utility-types/objects"
Properties<Obj1, Obj2, Common = false>
Creates a union of the keys of two objects. If Common
is true
, returns only the common keys.
interface Foo {
foo: string
}
interface Bar {
bar: number
}
// Expected: "foo" | "bar"
type PropsFooBar = Properties<Foo, Bar>
Intersection<Obj1, Obj2>
Creates a new object with the keys that are in one but not the other.
interface Foo {
name: string
age: string
}
interface Bar {
name: string
age: string
gender: number
}
// Expected: { gender: number }
type DiffFoo = Intersection<Foo, Bar>
PickByType<Obj, Type>
Selects the properties of an object that are assignable to a given type.
interface User {
name: string
lastname: string
age: number
}
// Expected: { name: string, lastname: string }
type UserStr = PickByType<User, string>
PartialByKeys<Obj, Keys = keyof Obj>
Makes the specified keys of an object optional (all keys by default).
interface User {
name: string
lastname: string
age: number
}
// Expected: { name?: string, lastname: string, age: number }
type UserPartialName = PartialByKeys<User, "name">
OmitByType<Obj, Type>
Omits the properties of an object that are assignable to a given type.
interface User {
name: string
lastname: string
age: number
}
// Expected: { age: number }
type UserExcludeStrings = OmitByType<User, string>
FlattenProperties<Obj, Keys>
Extracts the value of a key and merges it into the object (experimental).
interface User {
name: string
lastname: string
address: { street: string; avenue: string }
}
// Expected: { name: string, lastname: string, street: string, avenue: string }
type UserAddress = FlattenProperties<User, "address">
ExcludePrivateKeys<Obj>
Removes properties whose name starts with an underscore (_
).
interface User {
_private: string
public: number
}
// Expected: { public: number }
type PublicUser = ExcludePrivateKeys<User>
RequiredByKeys<Obj, Keys = keyof Obj>
Makes the specified keys of an object required (all keys by default).
interface User {
name?: string
age?: number
address?: string
}
// Expected: { name: string, age?: number, address?: string }
type UserRequiredName = RequiredByKeys<User, "name">
Mutable<Obj>
Makes the properties of an object mutable (removes readonly
). Affects only the first level.
interface User {
readonly name: string
readonly lastname: string
readonly age: number
}
// Expected: { name: string, lastname: string, age: number }
type NonReadonlyUser = Mutable<User>
AppendKeyValue<Obj, Property, value>
Adds a new property to an object.
interface User {
name: string
}
// Expected: { name: string, lastname: string }
type UserAppendLastname = AppendKeyValue<User, "lastname", string>
ObjectEntries<Obj>
Returns a union of the [key, value]
entries of the object.
interface Foo {
foo: string
bar: number
foobar?: boolean
}
// Expected: ["foo", string] | ["bar", number] | ["foobar", boolean]
type FooEntries = ObjectEntries<Foo>
ReplaceKeys<Obj, Keys, Replace>
Replaces the types of the specified keys with those defined in Replace
.
interface Foo {
foo: string
bar: number
foobar: boolean
}
// Expected: { foo: number, bar: number, foobar: number }
type ReplaceStrings = ReplaceKeys<Foo, "foo" | "foobar", { foo: number; foobar: number }>
MapTypes<Obj, Mapper>
Maps the types of the properties that match from
in the Mapper
, replacing them with to
.
// Expected: { foo: string, bar: boolean }
type ReplaceTypesI = MapTypes<{ foo: string; bar: number }, { from: number; to: boolean }>
ToPrimitive<Obj>
Converts the properties of an object to their primitive types.
interface User {
name: "Foo"
lastname: "Bar"
age: 30
}
// Expected: { name: string, lastname: string, age: number }
type UserPrimitive = ToPrimitive<User>
Get<Obj1, Obj2, Key>
Returns the value of a key in one of the two objects.
interface Foo {
foo: string
}
interface Bar {
bar: number
}
// Expected: string
type FooValue = Get<Foo, Bar, "foo">
// Expected: number
type BarValue = Get<Foo, Bar, "bar">
GetRequired<Obj>
Returns only the required keys of an object.
interface User {
name: string
age?: number
address?: string
}
// Expected: { name: string }
type UserRequired = GetRequired<User>
GetOptional<Obj>
Returns only the optional keys of an object.
interface User {
name: string
age?: number
address?: string
}
// Expected: { age?: number, address?: string }
type UserOptional = GetOptional<User>