### Install SimplyTyped with npm Source: https://github.com/andnp/simplytyped/blob/master/README.md Installs the SimplyTyped library as a development dependency using npm. This command is typically run in the root of a Node.js project. ```bash npm install --save-dev simplytyped ``` -------------------------------- ### Install SimplyTyped Source: https://context7.com/andnp/simplytyped/llms.txt Installs the SimplyTyped library using npm for development. Also shows how to import types directly for Deno environments. ```bash npm install --save-dev simplytyped ``` ```typescript // For Deno import { Omit, Merge } from 'https://unpkg.com/simplytyped/edition-deno/index.ts'; ``` -------------------------------- ### Run Tests Against All Supported TypeScript Versions Source: https://github.com/andnp/simplytyped/blob/master/CONTRIBUTING.md Installs project dependencies and then executes all tests against every supported version of TypeScript. This ensures broad compatibility and catches version-specific issues. ```bash npm install npm run test:all ``` -------------------------------- ### ArgsAsTuple: Get Function Arguments as Tuple Type (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Extracts the argument types of a function and returns them as a tuple type. This utility supports functions with up to 7 parameters. ```typescript test("Can get a tuple of function's argument types", t => { type F0 = () => any; type F1 = (x: number) => any; type F2 = (x: number, y: string) => any; type F3 = (x: number, y: string, z: boolean) => any; type E0 = []; type E1 = [number]; type E2 = [number, string]; type E3 = [number, string, boolean]; assert, E0>(t); assert, E1>(t); assert, E2>(t); assert, E3>(t); }); ``` -------------------------------- ### Document Helper Type with JSDoc (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/CONTRIBUTING.md An example of documenting a helper type in TypeScript using JSDoc comments. The 'no-doc' tag can be used to exclude this specific helper from public documentation generation. ```typescript /** * no-doc * This helpful helper helps do anything. */ export type HelpfulHelper = any; ``` -------------------------------- ### Run Tests Locally with Specific TypeScript Version Source: https://github.com/andnp/simplytyped/blob/master/CONTRIBUTING.md Installs dependencies and a specific version of TypeScript (3.0.3) to run tests locally. This is useful for ensuring compatibility with older TypeScript versions or for debugging specific issues. ```bash npm install npm install typescript@3.0.3 npm test ``` -------------------------------- ### Get All Keys Between Objects in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md Demonstrates how to use the `AllKeys` utility type to retrieve all unique keys present in two different object types. This is useful for type-level operations involving object merging or comparison. ```typescript test('Can get all keys between objects', t => { type a = { w: number, x: string }; type b = { x: number, z: boolean }; type got = AllKeys; type expected = 'w' | 'x' | 'z'; assert(t); assert(t); }); ``` -------------------------------- ### Get Shared Keys Between Objects (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Retrieves all keys that are present in both of the provided object types. This is useful for identifying common properties between different data structures. ```typescript test('Can get keys that are same between objects', t => { type a = { x: number, y: string }; type b = { x: string, y: string, z: boolean }; type got = SharedKeys; type expected = 'x' | 'y'; assert(t); assert(t); }); ``` -------------------------------- ### Get All Combined Keys Source: https://context7.com/andnp/simplytyped/llms.txt The `AllKeys` utility type computes the union of all keys from two provided object types. It effectively merges all unique keys from both objects. ```typescript import { AllKeys } from 'simplytyped'; type A = { x: number; y: string }; type B = { y: number; z: boolean }; type Combined = AllKeys; // 'x' | 'y' | 'z' ``` -------------------------------- ### Length: Get Tuple Length (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Calculates the length of a tuple type. It returns a number literal representing the number of elements in the tuple. ```typescript test('Can get the length of a tuple', t => { type t = [1, 2, 3, 4]; type x = ['hello', 'world']; type gotT = Length; type gotX = Length; assert(t); assert(t); }); ``` -------------------------------- ### Filter Object Keys by Type (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Gets all keys from an object that point to a specific type or union of types. This is useful for selectively extracting keys based on their associated value types. ```typescript test('Can filter object keys by right side type', t => { type obj = { a: 1, b: 2, c: 3, }; type expected = 'a' | 'b'; type got = KeysByType; assert(t); assert(t); }); ``` -------------------------------- ### UnionKeys: Get All Keys Between Union Objects (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Computes the union of all keys present across multiple object types within a union. This is useful for defining types that must accommodate properties from any of the object types in the union. ```typescript test('Can get all keys between objects in a union', t => { type a = { w: number, x: string }; type b = { x: number, z: boolean }; type c = { y: boolean, z: string }; type got = UnionKeys; type expected = 'w' | 'x' | 'y' | 'z'; assert(t); assert(t); }); ``` -------------------------------- ### UnionizeProperties: Get Union of Object Values (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Extracts a union of all possible values from the properties of an object type. This is helpful when you need to refer to the set of all possible values an object's properties can hold. ```typescript test('Can get a union of all values in an object', t => { type a = { x: 'hi', y: 'there', z: 'friend' }; type got = UnionizeProperties; type expected = 'hi' | 'there' | 'friend'; assert(t); assert(t); }); ``` -------------------------------- ### Safely Get Property Type Source: https://context7.com/andnp/simplytyped/llms.txt The `GetKey` utility type safely retrieves the type of a property from an object. If the key does not exist on the object, it returns `never`. This helps in type-safe property access. ```typescript import { GetKey } from 'simplytyped'; type User = { name: string; age: number }; type NameType = GetKey; // string type MissingType = GetKey; // never ``` -------------------------------- ### objectKeys: Get an object's keys with a precise array type in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The `objectKeys` function is similar to `Object.keys` but returns a typed array of the object's keys. It provides a more accurate type inference for the keys compared to the default `string[]`. However, it is not safe for untrusted input due to potential runtime behavior differences from native `Object.keys`. ```typescript test('Can get keys of an object', t => { const o = { a: 'hi', b: 22 }; const keys = objectKeys(o); type K = typeof keys; type expected = Array<'a' | 'b'>; assert(t); assert(t); t.deepEqual(keys, ['a', 'b']); }); ``` -------------------------------- ### Get Union of Property Values Source: https://context7.com/andnp/simplytyped/llms.txt The `UnionizeProperties` utility type creates a union of all the value types of the properties within an object type. It simplifies getting a collective type for all property values. ```typescript import { UnionizeProperties } from 'simplytyped'; type Config = { host: string; port: number; secure: boolean }; type ConfigValues = UnionizeProperties; // string | number | boolean ``` -------------------------------- ### Get Keys by Value Type Source: https://context7.com/andnp/simplytyped/llms.txt The `KeysByType` utility type extracts all keys from an object whose corresponding value types match a specified type. It returns a union of the matching keys. ```typescript import { KeysByType } from 'simplytyped'; type Mixed = { id: number; name: string; count: number; description: string; active: boolean; }; type NumberKeys = KeysByType; // 'id' | 'count' type StringKeys = KeysByType; // 'name' | 'description' ``` -------------------------------- ### Get All Union Keys Source: https://context7.com/andnp/simplytyped/llms.txt The `UnionKeys` utility type extracts all unique keys from all members of a given union type. It aggregates keys from disparate object types within a union. ```typescript import { UnionKeys } from 'simplytyped'; type Dog = { name: string; breed: string }; type Cat = { name: string; lives: number }; type Fish = { name: string; waterType: 'fresh' | 'salt' }; type AllAnimalKeys = UnionKeys; // 'name' | 'breed' | 'lives' | 'waterType' ``` -------------------------------- ### Get Keys Only in First Object Source: https://context7.com/andnp/simplytyped/llms.txt The `DiffKeys` utility type returns a union of keys that exist in the first object type but not in the second object type. It highlights unique properties of the first object. ```typescript import { DiffKeys } from 'simplytyped'; type Full = { a: number; b: string; c: boolean }; type Partial = { b: string }; type OnlyInFull = DiffKeys; // 'a' | 'c' ``` -------------------------------- ### Type-Safe Object Keys with objectKeys Source: https://context7.com/andnp/simplytyped/llms.txt The objectKeys function provides a type-safe way to get the keys of an object, returning an array of keys that TypeScript understands. This prevents runtime errors by ensuring that the keys used to access object properties are valid. ```typescript import { objectKeys } from 'simplytyped'; const user = { name: 'John', age: 30, email: 'john@example.com' }; const keys = objectKeys(user); // Array<'name' | 'age' | 'email'> keys.forEach(key => { console.log(user[key]); // TypeScript knows this is valid }); ``` -------------------------------- ### Get Shared Keys Between Objects Source: https://context7.com/andnp/simplytyped/llms.txt The `SharedKeys` utility type identifies and returns a union of keys that are present in both of the provided object types. This is useful for finding common properties. ```typescript import { SharedKeys } from 'simplytyped'; type A = { x: number; y: string; z: boolean }; type B = { y: number; z: string; w: boolean }; type Common = SharedKeys; // 'y' | 'z' ``` -------------------------------- ### Get the previous number (n - 1) in TypeScript Source: https://context7.com/andnp/simplytyped/llms.txt The Prev utility type returns the predecessor of a given literal number type (n - 1). It's useful for decrementing numbers at the type level. ```typescript import { Prev } from 'simplytyped'; type Zero = Prev<1>; // 0 type Nine = Prev<10>; // 9 ``` -------------------------------- ### Get the next number (n + 1) in TypeScript Source: https://context7.com/andnp/simplytyped/llms.txt The Next utility type returns the successor of a given literal number type (n + 1). It's a simple utility for incrementing numbers at the type level. ```typescript import { Next } from 'simplytyped'; type Two = Next<1>; // 2 type Ten = Next<9>; // 10 ``` -------------------------------- ### Get the length of a tuple in TypeScript Source: https://context7.com/andnp/simplytyped/llms.txt The Length utility type calculates the number of elements in a tuple type and returns it as a literal number type. It works for both homogeneous and heterogeneous tuples, as well as empty tuples. ```typescript import { Length } from 'simplytyped'; type ThreeItems = Length<[string, number, boolean]>; // 3 type Empty = Length<[]>; // 0 ``` -------------------------------- ### Import SimplyTyped for Deno Source: https://github.com/andnp/simplytyped/blob/master/README.md Imports the SimplyTyped library for use with Deno. This import statement points to the Deno-specific edition hosted on unpkg. ```typescript import { ... } from 'https://unpkg.com/simplytyped/edition-deno/index.ts'; ``` -------------------------------- ### Combine Objects in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md Shows how to use the `CombineObjects` utility type to merge two object types into a single intersection type. It cleans up the resulting type, making it more readable in IDEs like VSCode by avoiding the display of `&` symbols for complex intersections. ```typescript test('Can combine two objects (without pesky & in vscode)', t => { type a = { x: number, y: 'hi' }; type b = { z: number }; type got = CombineObjects; type expected = { x: number, y: 'hi', z: number }; assert(t); assert(t); }); ``` -------------------------------- ### Make All Fields Required in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md Illustrates the use of the `AllRequired` utility type to transform an object type, ensuring all its properties are marked as required and not optional or nullable. This is useful for enforcing stricter object structures. ```typescript test('Can make all fields of options object required (not optional and not nullable)', t => { type x = { a?: string, b: number | undefined }; type got = AllRequired; type expected = { a: string, b: number }; assert(t); assert(t); }); ``` -------------------------------- ### Nand: Implement logical NAND for boolean types in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The Nand type utility implements a logical NAND (Not AND) operation for two boolean types. It returns `False` only if both input types are `True`, otherwise it returns `True`. This is useful for specific logical constructs. ```typescript test('Conditions can be based on NAND', t => { assert, False>(t); assert, True>(t); assert, True>(t); assert, True>(t); }); ``` -------------------------------- ### Build Constructor Type Source: https://context7.com/andnp/simplytyped/llms.txt The `ConstructorFor` utility type generates a constructor type for a given object type. This allows for creating instances of objects using their constructor function. ```typescript import { ConstructorFor } from 'simplytyped'; type User = { name: string; age: number }; type UserConstructor = ConstructorFor; function createInstance(ctor: ConstructorFor, ...args: any[]): T { return new ctor(...args); } ``` -------------------------------- ### ConstructorFunction: Type for Constructor Functions (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Represents the type of a constructor function for a given object type. It allows you to create type definitions for classes or constructor functions. ```typescript test('Can build a constructor type for a type', t => { type Constructor = ConstructorFunction<{ x: string, y: number }>; class Thing { x: string = ''; y: number = 22; } assert(t); }); ``` -------------------------------- ### PromiseOr: Union with Promise (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Returns a type that is either the original type or a `Promise` wrapping that type. This is useful for functions that might return a value directly or asynchronously. ```typescript test('Will give back a promise containing given type union the type itself', t => { type got = PromiseOr; type expected = Promise | string; assert(t); }); ``` -------------------------------- ### IsZero: Check if Number is Zero (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Checks if a given number literal is equal to zero. It returns `True` if the number is 0, and `False` otherwise. ```typescript test('Can check if a number is zero', t => { type notZero = IsZero<1>; type zero = IsZero<0>; assert(t); assert(t); }); ``` -------------------------------- ### Represent Constructor Functions with ConstructorFunction Source: https://context7.com/andnp/simplytyped/llms.txt ConstructorFunction defines a type for constructor functions that create instances of a specific type. It's used for generic class instantiation. Dependencies: 'simplytyped'. ```typescript import { ConstructorFunction } from 'simplytyped'; type UserCtor = ConstructorFunction<{ name: string; id: number }>; function createMany(ctor: ConstructorFunction, count: number): T[] { return Array.from({ length: count }, () => new ctor()); } ``` -------------------------------- ### Extract Function Arguments as Tuple with ArgsAsTuple Source: https://context7.com/andnp/simplytyped/llms.txt ArgsAsTuple extracts the parameter types of a function into a tuple. This is helpful for creating functions that accept arguments as a tuple. Dependencies: 'simplytyped'. ```typescript import { ArgsAsTuple } from 'simplytyped'; type Greet = (name: string, age: number, active: boolean) => void; type GreetArgs = ArgsAsTuple; // [string, number, boolean] function callWith any>(fn: F, args: ArgsAsTuple): ReturnType { return fn(...args); } ``` -------------------------------- ### GetKey: Safely Access Object Property Values by Key (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md The GetKey type safely retrieves the value of a specified property from an object type. It returns the type of the property's value if the key exists, and `never` if the key is not present on the object. It's recommended to use with `If { type obj = { x: number, y: string }; type expected = number; type got = GetKey; assert(t); assert(t); }); test('Will get `never` if key does not exist', t => { type obj = { x: number, y: string }; type expected = never; type got = GetKey; assert(t); assert(t); }); ``` -------------------------------- ### IsOne: Check if Number is One (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Checks if a given number literal is equal to one. It returns `True` if the number is 1, and `False` otherwise. ```typescript test('Can check if a number is one', t => { type notOne = IsOne<0>; type one = IsOne<1>; assert(t); assert(t); }); ``` -------------------------------- ### DiffKeys: Find Different Keys Between Two Objects (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md The DiffKeys type calculates the set difference between the keys of two object types. It returns a union of keys that exist in the first type but not in the second. The order of types matters, as `DiffKeys` is not the same as `DiffKeys`. Dependencies: None. ```typescript test('Can get all keys that are different between objects', t => { type a = { x: number, y: string }; type b = { y: string, z: number }; type gotA = DiffKeys; type gotB = DiffKeys; assert(t); assert(t); }); ``` -------------------------------- ### taggedObject: Generate tagged unions of objects in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The `taggedObject` utility is designed for creating tagged unions of objects. It automatically adds a `name` property to each sub-object, where the `name` is the key pointing to that sub-object. This is particularly useful for implementing patterns like Redux reducers. ```typescript test('Can generate a tagged object', t => { const obj = { a: { merp: 'hi' }, b: { merp: 'there' }, c: { merp: 'friend' }, }; const expected = { a: { name: 'a' as 'a', merp: 'hi' }, b: { name: 'b' as 'b', merp: 'there' }, c: { name: 'c' as 'c', merp: 'friend' }, }; const got = taggedObject(obj, 'name'); t.deepEqual(got, expected); assert(t); assert(t); }); ``` -------------------------------- ### Add: Add Two Numbers (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Adds two number literals together at the type level. This utility is useful for performing arithmetic operations during compile time. ```typescript test('Can add two numbers', t => { type fifty = Add<12, 38>; assert(t); }); ``` -------------------------------- ### AnyFunc: Define Arbitrary Function Types (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Defines a type for an arbitrary function. It can be used to specify a function that accepts any arguments and returns any type, or a specific return type. ```typescript test('Can define the type of a function that takes any arguments', t => { type got = AnyFunc; type got2 = AnyFunc; // takes anything, returns a number type expected = (...args: any[]) => any; type expected2 = (...args: any[]) => number; assert(t); assert(t); }); ``` -------------------------------- ### Xor: Implement logical XOR for boolean types in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The Xor type utility implements a logical XOR (Exclusive OR) operation for two boolean types. It returns `True` if exactly one of the input types is `True`, otherwise it returns `False`. This is useful for specific conditional logic. ```typescript test('Conditions can be based on XOR', t => { assert, False>(t); assert, True>(t); assert, True>(t); assert, False>(t); }); ``` -------------------------------- ### And: Implement logical AND for boolean types in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The And type utility implements a logical AND operation for two boolean types. It returns `True` only if both input types are `True`, otherwise it returns `False`. This is useful for creating complex conditional types. ```typescript test('Conditions can be based on AND', t => { type conditional = If, number, string>; type gotFF = conditional; type gotFT = conditional; type gotTF = conditional; type gotTT = conditional; assert(t); assert(t); assert(t); assert(t); }); ``` -------------------------------- ### Nullable: Mark Type as Nullable (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Makes a given type nullable by adding `null` and `undefined` to its possible types. This is a convenient shorthand for `T | null | undefined`. ```typescript test('Will make a type nullable (null | undefined)', t => { type got = Nullable; type expected = string | null | undefined; assert(t); }); test('Will make a type not nullable', t => { type got = NonNullable>; assert(t); }); ``` -------------------------------- ### Define Arbitrary Function Types with AnyFunc Source: https://context7.com/andnp/simplytyped/llms.txt The AnyFunc type allows defining functions with optional return types. It's useful for creating generic function types and wrappers. Dependencies: 'simplytyped'. ```typescript import { AnyFunc } from 'simplytyped'; type Callback = AnyFunc; // (...args: any[]) => any type NumberReturning = AnyFunc; // (...args: any[]) => number function wrapWithLogging(fn: F): F { return ((...args: any[]) => { console.log('Called with:', args); return fn(...args); }) as F; } ``` -------------------------------- ### Convert Type to Object Type (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Takes any given type and transforms it into an object type. This is particularly useful when used with intersection types (`&`) to ensure a consistent object structure. ```typescript test('Can turn an object into another object', t => { type obj = { x: number, y: string }; type expected = obj; type got = ObjectType; assert(t); assert(t); }); ``` -------------------------------- ### isKeyOf: Runtime type guard for object keys in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The `isKeyOf` function acts as a runtime type guard. It checks if a given key `k` is present in an object `obj`. If `k` is a key of `T`, it narrows the type of `k` to a union of the keys of `T`. This improves type safety when accessing object properties dynamically. ```typescript test('Can check if an object contains a key', t => { const o = { a: 'hi', b: 22 }; const key1: string = 'a'; if (isKeyOf(o, key1)) { assert(t); t.pass(); } else { assert(t); t.fail(); } }); ``` -------------------------------- ### Type-Level NAND Operation with Nand Source: https://context7.com/andnp/simplytyped/llms.txt The Nand type performs a logical NAND (NOT AND) operation on two boolean types. It returns '0' (False) only if both inputs are '1' (True). Dependencies: 'simplytyped', 'True', 'False'. ```typescript import { Nand, True, False } from 'simplytyped'; type NotBoth = Nand; // '0' (False) type AtLeastOneFalse = Nand; // '1' (True) ``` -------------------------------- ### StringEqual: Check Equality of String Unions (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Checks if two union types of string literals are equal. It returns `True` if they contain the exact same set of string literals, regardless of order, and `False` otherwise. ```typescript test('Can check that two unions of strings are equal', t => { type a = 'hi' | 'there'; type b = 'there' | 'hi'; type c = 'hi' | 'there' | 'friend'; assert, True>(t); assert, True>(t); assert, False>(t); }); ``` -------------------------------- ### Readonly: Create readonly object literals with type inference in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The `Readonly` utility helps in marking object literals as readonly while preserving type inference. This is useful for ensuring that object properties are not accidentally modified after creation, enhancing immutability. ```typescript const obj = Readonly({ a: 22, b: 'yellow' }); ``` -------------------------------- ### Not: Implement logical NOT for boolean types in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The Not type utility inverts a given boolean type. If the input is `True`, it returns `False`, and if the input is `False`, it returns `True`. This is essential for negating conditions. ```typescript test('Conditional logic can be inversed with NOT', t => { type conditional = If, number, string>; type gotF = conditional; type gotT = conditional; assert(t); assert(t); }); ``` -------------------------------- ### Make Object Properties Optional (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Marks specific keys of a given type `T` as optional. This utility is similar in functionality to the built-in `Partial` utility but allows for selective optionality. ```typescript test('Can make properties optional', t => { type x = { x: number, y: string, z: 'hello there' }; type expected = { x?: number, y?: string, z: 'hello there' }; type got = Optional; assert(t); assert(t); }); ``` -------------------------------- ### Make Object Properties Required (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Marks specific keys of a given type `T` as required. This utility is useful for ensuring that certain properties are always present in an object. ```typescript test('Can make certain fields of options object required', t => { type x = { a?: string, b: number | undefined }; type got1 = Required; type got2 = Required; type got3 = Required; type expected1 = { a: string, b: number | undefined }; type expected2 = { a?: string, b: number }; type expected3 = { a: string, b: number }; assert(t); assert(t); assert(t); }); ``` -------------------------------- ### If: Implement conditional type assignment in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The If type utility allows for conditional type assignment based on a boolean condition. If the condition is `True`, it returns the `True` type; otherwise, it returns the `False` type. This is a fundamental building block for conditional logic in TypeScript. ```typescript test('Can assign type conditionally', t => { type conditional = If; type gotF = conditional; type gotT = conditional; assert(t); assert(t); }); ``` -------------------------------- ### Check if a type is 'null' or 'undefined' in TypeScript Source: https://context7.com/andnp/simplytyped/llms.txt The IsNil utility type checks if a type is either 'null' or 'undefined'. It returns '1' if the type is 'null' or 'undefined', and '0' otherwise. This is useful for ensuring that variables or function return values can be null or undefined. ```typescript import { IsNil } from 'simplytyped'; type NullCheck = IsNil; // '1' (True) type UndefinedCheck = IsNil; // '1' (True) type NotNil = IsNil; // '0' (False) ``` -------------------------------- ### Make Properties Optional Source: https://context7.com/andnp/simplytyped/llms.txt The `Optional` utility type makes specified properties of an object type optional, leaving other properties unchanged. It takes the object type and a union of keys to make optional as arguments. ```typescript import { Optional } from 'simplytyped'; type User = { id: number; name: string; bio: string }; type CreateUserInput = Optional; // Result: { id?: number; name: string; bio?: string } ``` -------------------------------- ### StrictUnion: Disallow Union Members with Mixed Properties (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Ensures that members of a union type do not include keys from other members, enforcing stricter union definitions. This is useful for discriminated unions where each member should have a distinct set of properties. ```typescript test('disallow union members with mixed properties', t => { type a = { a: number }; type b = { b: string }; type good1 = {a: 1}; type good2 = {b: "b"}; type bad = {a: 1, b: "foo"}; type isStrict = T extends Array> ? 'Yes' : 'No'; type strictUnion = [good1, good2]; type nonStrictUnion = [good1, good2, bad]; assert, 'Yes'>(t); assert, 'No'>(t); }); ``` -------------------------------- ### Or: Implement logical OR for boolean types in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The Or type utility implements a logical OR operation for two boolean types. It returns `True` if at least one of the input types is `True`, otherwise it returns `False`. This is useful for combining conditions. ```typescript test('Conditions can be based on OR', t => { type conditional = If, number, string>; type gotFF = conditional; type gotFT = conditional; type gotTF = conditional; type gotTT = conditional; assert(t); assert(t); assert(t); assert(t); }); ``` -------------------------------- ### Sub: Subtract two numbers in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The Sub type utility subtracts the second number type from the first. It returns the resulting number type. This is useful for performing arithmetic operations at compile time. ```typescript test('Can subtract two numbers', t => { type ten = Sub<22, 12>; assert(t); }); ``` -------------------------------- ### AllRequired: Make All Properties Required in TypeScript Source: https://context7.com/andnp/simplytyped/llms.txt Ensures all properties of an object type are required and removes `null` or `undefined` from their possible types. This is useful for enforcing complete data structures. ```typescript import { AllRequired } from 'simplytyped'; type PartialUser = { name?: string; age?: number | undefined; email?: string | null }; type CompleteUser = AllRequired; // Result: { name: string; age: number; email: string } ``` -------------------------------- ### Type-Level AND Operation with And Source: https://context7.com/andnp/simplytyped/llms.txt The And type performs a logical AND operation on two boolean types. It returns '1' (True) only if both inputs are '1' (True). Dependencies: 'simplytyped', 'True', 'False', 'If'. ```typescript import { And, True, False, If } from 'simplytyped'; type BothTrue = And; // '1' (True) type OneFalse = And; // '0' (False) type AllFalse = And; // '0' (False) type RequiresBoth = If, 'allowed', 'denied'>; ``` -------------------------------- ### Nominal: Construct Nominal Types (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Creates a nominal type based on an existing type `T` and a unique tag. This prevents values of the same base type but different nominal types from being assigned to each other, improving type safety for distinct concepts like IDs. ```typescript test('Can make a new nominal type', t => { type Id = Nominal; // TODO: improve once negative testing is in place assert>(t); }); ``` -------------------------------- ### Mark a type as potentially null or undefined in TypeScript Source: https://context7.com/andnp/simplytyped/llms.txt The Nullable utility type creates a new type that is the original type combined with 'null' and 'undefined'. This is useful for indicating that a value might be absent or not yet initialized. ```typescript import { Nullable } from 'simplytyped'; type MaybeString = Nullable; // string | null | undefined function getValue(): Nullable { return Math.random() > 0.5 ? 42 : null; } ``` -------------------------------- ### Create Tagged Unions with taggedObject Source: https://context7.com/andnp/simplytyped/llms.txt taggedObject is a utility for creating tagged unions from object records, commonly used for Redux-style actions. It automatically adds a 'type' property to each action object, ensuring type safety and consistency. ```typescript import { taggedObject } from 'simplytyped'; const actions = taggedObject({ increment: { amount: 1 }, decrement: { amount: 1 }, reset: {} }, 'type'); // Result: // { // increment: { type: 'increment', amount: 1 }, // decrement: { type: 'decrement', amount: 1 }, // reset: { type: 'reset' } // } type Action = typeof actions[keyof typeof actions]; // Action = { type: 'increment'; amount: number } | { type: 'decrement'; amount: number } | { type: 'reset' } ``` -------------------------------- ### HasKey: Check for Key Existence in an Object Type (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md The HasKey type checks if a given key `K` is present in a type `T`. It returns `True` if the key exists and `False` otherwise. This is a fundamental utility for conditional type logic involving object properties. Dependencies: None. -------------------------------- ### Create Tagged Object Source: https://context7.com/andnp/simplytyped/llms.txt The `TaggedObject` utility type transforms an object type by adding a tag property to each of its members. The tag property's value is the key name of the original member, useful for creating tagged unions. ```typescript import { TaggedObject } from 'simplytyped'; type Actions = { increment: { amount: number }; decrement: { amount: number }; reset: {}; }; type TaggedActions = TaggedObject; // Result: // { // increment: { type: 'increment'; amount: number }; // decrement: { type: 'decrement'; amount: number }; // reset: { type: 'reset' }; // } ``` -------------------------------- ### Check if Key Exists Source: https://context7.com/andnp/simplytyped/llms.txt The `HasKey` utility type checks if a given key exists within an object type, returning '1' for true and '0' for false. It can be used with `If` for conditional type logic. ```typescript import { HasKey, If } from 'simplytyped'; type User = { name: string; email: string }; type HasName = HasKey; // '1' (True) type HasAge = HasKey; // '0' (False) type GetPropOrDefault = If, GetKey, D>; ``` -------------------------------- ### Check for exact string union equality in TypeScript Source: https://context7.com/andnp/simplytyped/llms.txt The StringEqual utility type compares two union of string literal types for exact equality, ignoring the order of elements. It returns '1' (True) if they are identical and '0' (False) otherwise. This is useful for ensuring that two string sets are precisely the same. ```typescript import { StringEqual, True, False } from 'simplytyped'; type Same = StringEqual<'a' | 'b', 'b' | 'a'>; // '1' (True) type Different = StringEqual<'a' | 'b', 'a' | 'c'>; // '0' (False) ``` -------------------------------- ### Check if a number is zero in TypeScript Source: https://context7.com/andnp/simplytyped/llms.txt The IsZero utility type checks if a given literal number type is zero. It returns '1' (True) if the number is 0 and '0' (False) otherwise. This is useful for conditional types involving zero values. ```typescript import { IsZero, True, False } from 'simplytyped'; type ZeroCheck = IsZero<0>; // '1' (True) type NotZero = IsZero<5>; // '0' (False) ``` -------------------------------- ### OverwriteReturn: Modify Function Return Type (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Modifies the return type of a function while keeping its parameters the same. This utility supports functions with up to 7 parameters. ```typescript test('Can change return type of a function', t => { type f = (x: 'hi', y: 'there', z: 22) => number; type got = OverwriteReturn; type expected = (x: 'hi', y: 'there', z: 22) => string; assert(t); assert(t); }); ``` -------------------------------- ### Check for String Type with IsString Source: https://context7.com/andnp/simplytyped/llms.txt IsString checks if a given type is a string literal or type. It returns '1' (True) for string types and '0' (False) otherwise. Dependencies: 'simplytyped'. ```typescript import { IsString } from 'simplytyped'; type StringCheck = IsString<'hello'>; // '1' (True) type NotString = IsString<42>; // '0' (False) ``` -------------------------------- ### NumberEqual: Check if two numbers are equal in TypeScript Source: https://github.com/andnp/simplytyped/blob/master/README.md The NumberEqual type utility checks if two number types are equivalent. It returns `True` if they are equal and `False` otherwise. This is useful for compile-time assertions. ```typescript test('Can check if two numbers are equal', t => { type notEqual = NumberEqual<22, 23>; type equal = NumberEqual<12, 12>; assert(t); assert(t); }); ``` -------------------------------- ### Check if a type is 'any' in TypeScript Source: https://context7.com/andnp/simplytyped/llms.txt The IsAny utility type checks if a type is the 'any' type. It returns '1' if the type is 'any' and '0' otherwise. This can be used to identify potentially unsafe type usages. ```typescript import { IsAny } from 'simplytyped'; type AnyCheck = IsAny; // '1' (True) type NotAny = IsAny; // '0' (False) ``` -------------------------------- ### Check for Array Type with IsArray Source: https://context7.com/andnp/simplytyped/llms.txt IsArray checks if a given type is an array type. It returns '1' (True) for array types and '0' (False) otherwise. Useful for conditional type manipulation. Dependencies: 'simplytyped', 'If'. ```typescript import { IsArray, If } from 'simplytyped'; type ArrayCheck = IsArray; // '1' (True) type NotArray = IsArray; // '0' (False) type Flatten = If, T extends (infer U)[] ? U : never, T>; ``` -------------------------------- ### Overwrite Object Properties (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Allows for the modification of existing property types within an object. Unlike `Merge`, `Overwrite` will not introduce new properties that were not present in the original object. ```typescript test('Can overwrite properties on an object', t => { type a = { x: number, y: string, z: 'hello there' }; type expected = { x: number, y: string, z: 'hello' | 'there' }; type got1 = Overwrite; type got2 = Overwrite; assert(t); assert(t); assert(t); assert(t); }); ``` -------------------------------- ### Create a nominal/branded type in TypeScript Source: https://context7.com/andnp/simplytyped/llms.txt The Nominal utility type creates a branded type that is distinct from its base type, even if they have the same underlying structure. This enhances type safety by preventing accidental assignment between similar but conceptually different types. ```typescript import { Nominal } from 'simplytyped'; type UserId = Nominal; type PostId = Nominal; function getUser(id: UserId): void { /* ... */ } function getPost(id: PostId): void { /* ... */ } const userId = 'user-123' as UserId; const postId = 'post-456' as PostId; getUser(userId); // OK // getUser(postId); // Error: PostId is not assignable to UserId ``` -------------------------------- ### Merge Objects with Rightmost Conflict Resolution (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Merges two objects, combining all keys from both. When keys conflict, the value from the rightmost object takes precedence. This is similar to `_.merge` in JavaScript. ```typescript test('Can merge two objects, resolving matching keys by rightmost object', t => { type a = { x: number, y: string }; type b = { y: number, z: string }; type got = Merge; type expected = { x: number, y: number, z: string }; assert(t); assert(t); }); test('Can merge an object containing all strings as keys', t => { type a = { y: string; [s: string]: string; }; type b = { x: number, y: number }; type got = Merge; type expected = { x: number, y: number } & Record; assert(t); assert(t); }); ``` -------------------------------- ### UnionizeTuple: Unionize Tuple Values (TypeScript) Source: https://github.com/andnp/simplytyped/blob/master/README.md Creates a union type from all the values contained within a tuple. This is useful for creating a type that represents any of the elements present in a tuple. ```typescript test('Can get a union of all values in tuple', t => { type t = ['hi', 'there', 'friend']; type got = UnionizeTuple; type expected = 'hi' | 'there' | 'friend'; assert(t); assert(t); }); ``` -------------------------------- ### Convert a number type to its string representation in TypeScript Source: https://context7.com/andnp/simplytyped/llms.txt The NumberToString utility type converts a literal number type into its corresponding string literal type. This is useful when you need to use a number in a context that expects a string, at the type level. ```typescript import { NumberToString } from 'simplytyped'; type Str = NumberToString<42>; // '42' type Zero = NumberToString<0>; // '0' ```