@typedly/object v0.0.1
typedly/object
A TypeScript type definitions package to handle object-related operations.
Table of contents
Installation
Install peer dependencies.
npm install @typedly/property --save-peer
Install the package.
npm install @typedly/object --save-peer
Api
import {
AddProperty,
Exact,
GetProperty,
GetPropertyValue,
KeyValueSchema,
Keys,
ObjectToUnion,
PickKeys,
RemoveProperty,
SetProperty,
UnionToObject,
UpdateProperty,
} from '@typedly/object';
AddProperty
Differences between Add
and AddProperty
.
import { AddProperty } from '@typedly/object';
import { Add } from '@typedly/property';
const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type Example1 = Add<typeof object, 'city', 'London'>;
// type Example1 = { readonly firstName: "Someone"; readonly lastName: "Someone surname"; readonly age: 227; } & { city: "London"; }
type Example2 = AddProperty<typeof object, 'city', 'London'>;
// { readonly firstName: "Someone"; readonly lastName: "Someone surname"; readonly age: 227; city: "London"; }
Exact
import { Exact } from '@typedly/object';
const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type Example = Exact<typeof object>; // const example: { readonly firstName: "Someone"; readonly lastName: "Someone surname"; readonly age: 227;}
GetPropertyValue
import { GetPropertyValue } from '@typedly/object';
const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type Example1 = GetPropertyValue<typeof object, 'age'>;
// 227
GetProperty
import { GetProperty } from '@typedly/object';
const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type Example1 = GetProperty<typeof object, 'firstName'>;
// type Example1 = { readonly firstName: "Someone"; }
KeyValueSchema
import { KeyValueSchema } from '@typedly/object';
Keys
import { Keys } from '@typedly/object';
const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type Example1 = Keys<typeof object>; // "firstName" | "lastName" | "age"
ObjectToUnion
import { ObjectToUnion } from '@typedly/object';
type User = {
id: number;
name: string;
active: boolean;
};
type UserUnion = ObjectToUnion<User>;
/*
Equivalent to:
type UserUnion =
| { key: "id"; value: number }
| { key: "name"; value: string }
| { key: "active"; value: boolean };
*/
type Config = {
theme: "light" | "dark";
version: number;
debug: { enabled: boolean; level: number };
};
type ConfigUnion = ObjectToUnion<Config>;
/*
type ConfigUnion =
| { key: "theme"; value: "light" | "dark" }
| { key: "version"; value: number }
| { key: "debug"; value: { enabled: boolean; level: number } };
*/
type Settings = {
readonly mode: "auto" | "manual";
volume?: number;
};
type SettingsUnion = ObjectToUnion<Settings>;
/*
type SettingsUnion =
| { key: "mode"; value: "auto" | "manual" }
| { key: "volume"; value: number | undefined };
*/
PickKeys
import { PickKeys } from '@typedly/object';
type User = {
id: number;
name: string;
active: boolean;
};
type UserSubset = PickKeys<User, "id" | "name">;
/*
Equivalent to:
type UserSubset = {
id: number;
name: string;
};
*/
type Config = {
theme: "light" | "dark";
version: number;
debug: boolean;
};
type ConfigSubset = PickKeys<Config, "theme" | "unknown">;
/*
Equivalent to:
type ConfigSubset = {
theme: "light" | "dark";
};
*/
type Settings = {
mode: "auto" | "manual";
volume: number;
};
type InvalidSubset = PickKeys<Settings, "brightness" | "contrast">;
/*
Equivalent to:
type InvalidSubset = {}; // Empty object since no matching keys exist
*/
RemoveProperty
import { RemoveProperty } from '@typedly/object';
const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type Example1 = RemoveProperty<typeof object, 'firstName'>; // { readonly lastName: "Someone surname"; readonly age: 227; }
SetProperty
import { SetProperty } from '@typedly/object';
const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 as number } as const;
type Example1 = Set<{ firstName: 'Someone', lastName: 'Someone surname', age: 227 }, 'age', 227>;
type Example2 = SetProperty<typeof object, 'age', 222>;
// { readonly firstName: "Someone"; readonly lastName: "Someone surname"; readonly age: 227; city: "London"; }
UnionToObject
import { UnionToObject } from '@typedly/object';
type UserUnion =
| { key: "id"; value: number }
| { key: "name"; value: string }
| { key: "active"; value: boolean };
type UserObject = UnionToObject<UserUnion>;
/*
Equivalent to:
type UserObject = {
id: number;
name: string;
active: boolean;
};
*/
type ConfigUnion =
| { key: "theme"; value: "light" | "dark" }
| { key: "version"; value: number }
| { key: "debug"; value: { enabled: boolean; level: number } };
type ConfigObject = UnionToObject<ConfigUnion>;
/*
Equivalent to:
type ConfigObject = {
theme: "light" | "dark";
version: number;
debug: { enabled: boolean; level: number };
};
*/
type SettingsUnion =
| { key: "mode"; value: "auto" | "manual" }
| { key: "volume"; value?: number };
type SettingsObject = UnionToObject<SettingsUnion>;
/*
Equivalent to:
type SettingsObject = {
mode: "auto" | "manual";
volume?: number;
};
*/
UpdateProperty
import { UpdateProperty } from '@typedly/object';
const object = { firstName: 'Someone' as string, lastName: 'Someone surname', age: 227 } as const;
type Example1 = UpdateProperty<typeof object, 'firstName', 'The name'>;
/*
{
readonly firstName: "The name";
readonly lastName: "Someone surname";
readonly age: 227;
}
*/
Contributing
Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.
Support
If you find this package useful and would like to support its and general development, you can contribute through one of the following payment methods. Your support helps maintain the packages and continue adding new.
Support via:
Thanks for your support!
Code of Conduct
By participating in this project, you agree to follow Code of Conduct.
GIT
Commit
Versioning
Given a version number MAJOR.MINOR.PATCH, increment the:
- MAJOR version when you make incompatible API changes,
- MINOR version when you add functionality in a backwards-compatible manner, and
- PATCH version when you make backwards-compatible bug fixes.
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
FAQ How should I deal with revisions in the 0.y.z initial development phase?
The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.
How do I know when to release 1.0.0?
If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.
License
MIT © typedly (license)
Packages
- @typedly/array: A TypeScript type definitions package to handle array-related operations.
- @typedly/callback: A TypeScript type definitions package for asynchronous and synchronous callback functions of various types.
- @typedly/character: A TypeScript type definitions package for various character types.
- @typedly/context: A TypeScript type definitions package for context data structures.
- @typedly/descriptor: A TypeScript type definitions package for property descriptor.
- @typedly/digit: A TypeScript type definitions package for digit types.
- @typedly/letter: A TypeScript type definitions package for handling letter types.
- @typedly/payload: A TypeScript type definitions package for payload data structures.
- @typedly/property: A TypeScript type definitions package to handle object property-related operations.
- @typedly/regexp: A TypeScript type definitions package for
RegExp
. - @typedly/symbol: A TypeScript type definitions package for various symbols.
6 months ago