CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ts-essentials

All essential TypeScript types in one place providing 70+ utility types and helper functions.

Pending
Overview
Eval results
Files

mark-wrapper-types.mddocs/

Mark Wrapper Types

Transform specific properties of objects by changing their modifiers (optional, readonly, required, writable). These types work on individual property keys rather than entire objects.

Capabilities

MarkOptional

Constructs a type by picking all properties from type Type where properties Keys are set as optional, meaning they aren't required.

type MarkOptional<Type, Keys extends keyof Type> = Prettify<
  Omit<Type, Keys> & Partial<Pick<Type, Keys>>
>;

Usage Example:

import type { MarkOptional } from "ts-essentials";

type User = {
  id: number;
  name: string;
  email: string;
  avatar: string;
};

type UserInput = MarkOptional<User, "id" | "avatar">;
// Result: { name: string; email: string; id?: number; avatar?: string; }

const newUser: UserInput = {
  name: "Alice",
  email: "alice@example.com"
  // id and avatar are optional
};

MarkReadonly

Constructs a type by picking all properties from type Type where properties Keys are set to readonly, meaning they cannot be reassigned.

type MarkReadonly<Type, Keys extends keyof Type> = Prettify<
  Omit<Type, Keys> & Readonly<Pick<Type, Keys>>
>;

Usage Example:

import type { MarkReadonly } from "ts-essentials";

type User = {
  id: number;
  name: string;
  email: string;
  lastLogin: Date;
};

type UserEntity = MarkReadonly<User, "id" | "lastLogin">;
// Result: { name: string; email: string; readonly id: number; readonly lastLogin: Date; }

const user: UserEntity = {
  id: 1,
  name: "Alice",
  email: "alice@example.com",
  lastLogin: new Date()
};

user.name = "Bob"; // OK
user.email = "bob@example.com"; // OK
// user.id = 2; // Error: Cannot assign to 'id' because it is a read-only property
// user.lastLogin = new Date(); // Error: Cannot assign to 'lastLogin' because it is a read-only property

MarkRequired

Constructs a type by picking all properties from type Type where properties Keys are set as required.

type MarkRequired<Type, Keys extends keyof Type> = Prettify<
  Omit<Type, Keys> & Required<Pick<Type, Keys>>
>;

Usage Example:

import type { MarkRequired } from "ts-essentials";

type UserPreferences = {
  theme?: string;
  language?: string;
  notifications?: boolean;
  privacy?: string;
};

type RequiredUserPreferences = MarkRequired<UserPreferences, "theme" | "language">;
// Result: { notifications?: boolean; privacy?: string; theme: string; language: string; }

const prefs: RequiredUserPreferences = {
  theme: "dark", // Required
  language: "en", // Required
  notifications: true // Optional
  // privacy is optional
};

MarkWritable

Constructs a type by picking all properties from type Type where properties Keys remove readonly modifier, meaning they can be reassigned.

type MarkWritable<Type, Keys extends keyof Type> = Prettify<
  Omit<Type, Keys> & Writable<Pick<Type, Keys>>
>;

Usage Example:

import type { MarkWritable } from "ts-essentials";

type ImmutableUser = {
  readonly id: number;
  readonly name: string;
  readonly email: string;
  readonly createdAt: Date;
};

type EditableUser = MarkWritable<ImmutableUser, "name" | "email">;
// Result: { readonly id: number; readonly createdAt: Date; name: string; email: string; }

const user: EditableUser = {
  id: 1,
  name: "Alice",
  email: "alice@example.com",
  createdAt: new Date()
};

user.name = "Bob"; // OK - name is now writable
user.email = "bob@example.com"; // OK - email is now writable
// user.id = 2; // Error: Cannot assign to 'id' because it is a read-only property
// user.createdAt = new Date(); // Error: Cannot assign to 'createdAt' because it is a read-only property

Types

type Writable<Type> = {
  -readonly [Key in keyof Type]: Type[Key];
};

type Prettify<Type> = Type extends Function
  ? Type
  : Extract<{
      [Key in keyof Type]: Type[Key];
    }, Type>;

Install with Tessl CLI

npx tessl i tessl/npm-ts-essentials

docs

array-tuple-types.md

basic-types.md

change-case-types.md

deep-wrapper-types.md

function-types.md

index.md

key-types.md

mark-wrapper-types.md

type-checkers.md

utility-functions.md

utility-types.md

tile.json