Core Utilities

String Utilities

Utilities for manipulating and analyzing string types in TypeScript. These types allow you to trim, join, search, replace, capitalize, and analyze strings at the type level.

String Utilities

Utilities for manipulating and analyzing string types in TypeScript. These types allow you to trim, join, search, replace, capitalize, and analyze strings at the type level using template literal types.

Installation

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

Usage

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


type  = .
  • Absolute
  • AllEquals
  • AnyOf
  • AppendKeyValue
  • ArgsFunction
  • ArrayToUnion
  • Capitalize
  • Chunk
  • CompareArrayLength
  • ConstructTuple
  • DeepFilter
  • DeepGet
  • DeepKeys
  • DeepMerge
  • DeepMergeAll
  • DeepMutable
  • DeepNonNullable
  • DeepNonNullish
  • DeepNullable
  • DeepOmit
  • DeepPartial
  • DeepPick
  • DeepReadonly
  • DeepReplace
  • DeepRequired
  • DeepSet
  • DeepTruncate
  • DeepUnion
  • Discard
  • DropChar
  • EndsWith
  • Equals
  • ExcludePrivateKeys
  • Expect
  • Falsy
  • Filter
  • FilterNonNullish
  • FindAll
  • Flatten
  • FlattenProperties
  • Get
  • GetOptional
  • GetRequired
  • HasDuplicates
  • Includes
  • IndexOf
  • IndexOfString
  • Intersection
  • IsAny
  • IsArray
  • IsEven
  • IsFunction
  • IsNegative
  • IsNever
  • IsObject
  • IsOdd
  • IsPositive
  • Join
  • LastIndexOf
  • LengthOfString
  • LiteralUnion
  • MapTypes
  • Merge
  • MergeAll
  • Mutable
  • Not
  • Nullish
  • NumberRange
  • ObjectEntries
  • OmitByType
  • ParseUrlParams
  • PartialByKeys
  • PickByType
  • Prettify
  • Primitive
  • PrimitiveNullish
  • Properties
  • Replace
  • ReplaceKeys
  • RequiredByKeys
  • ReturnTypeOf
  • Reverse
  • Size
  • StartsWith
  • Take
  • ToPrimitive
  • ToUnion
  • Trim
  • TrimLeft
  • TrimRight
  • Trunc
  • Uniques
  • WhiteSpaces
  • Zip

Text Manipulation

TrimLeft<Str, string>

Removes whitespace from the start of a string type.

ArgumentTypeDescription
StrstringThe string to trim from the left.
import type {  } from "@halvaradop/ts-utility-types"

type TrimmedLeft = <"  hello world  ">
type TrimmedLeft = "hello world "
type NoSpaces = <"hello world">
type NoSpaces = "hello world"

TrimRight<Str, string>

Removes whitespace from the end of a string type.

ArgumentTypeDescription
StrstringThe string to trim from the right.
import type {  } from "@halvaradop/ts-utility-types"

type TrimmedRight = <"hello world  ">
type TrimmedRight = "hello world"
type NoTrailing = <"hello world">
type NoTrailing = "hello world"

Trim<Str, string>

Removes whitespace from both ends of a string type. Combines the functionality of TrimLeft and TrimRight.

ArgumentTypeDescription
StrstringThe string to trim.

Related: TrimLeft, TrimRight

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

// Remove whitespace from both ends
type Trimmed = <"  hello world  ">
type Trimmed = "hello world"
// Already trimmed string type AlreadyTrimmed = <"no spaces">
type AlreadyTrimmed = "no spaces"
// Only leading spaces type LeadingSpaces = <" leading only">
type LeadingSpaces = "leading only"

Template Literal Types

String utilities leverage TypeScript's template literal types for compile-time string manipulation.

Capitalize<Str, string, FirstWord, boolean = true>

Capitalizes the first letter of a word and optionally lowercases the rest.

ArgumentTypeDescription
StrstringThe string to capitalize.
FirstWordboolean (default true)Whether to only capitalize the first word.
import type {  } from "@halvaradop/ts-utility-types"

type Capitalized = <"hello world">
type Capitalized = "Hello World"
type CapitalizeAll = <"hello world", false>
type CapitalizeAll = "hello World"

Join<Array, unknown[], Separator, number | string>

Joins the values of a tuple into a string, using the given separator.

| Argument | Type | Description | | ----------- | ----------- | -------------------------------- | -------------------------------------- | | Array | unknown[] | The array to join into a string. | | Separator | number | string | The separator to use between elements. |

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

type JoinedDash = <["a", "p", "p", "l", "e"], "-">
type JoinedDash = "a-p-p-l-e"
type JoinedSpace = <["Hello", "World"], " ">
type JoinedSpace = "Hello World"
type JoinedNumber = <[1, 2, 3], "|">
type JoinedNumber = never

StartsWith<Str, string, Match, string>

Checks if a string type starts with the given value.

ArgumentTypeDescription
StrstringThe string to check.
MatchstringThe substring to check for at start.
import type {  } from "@halvaradop/ts-utility-types"

type StartsWithAc = <"abc", "ac">
type StartsWithAc = false
type StartsWithAb = <"abc", "ab">
type StartsWithAb = true
type StartsWithHello = <"hello world", "hello">
type StartsWithHello = true

DropChar<Str, string, Match, string>

Removes all occurrences of a character from a string type.

ArgumentTypeDescription
StrstringThe string to remove chars from.
MatchstringThe character to remove.
import type {  } from "@halvaradop/ts-utility-types"

type NoSpaces = <" b u t t e r f l y ! ", " ">
type NoSpaces = "butterfly!"
type NoDashes = <"a-b-c-d", "-">
type NoDashes = "abcd"

EndsWith<Str, string, Match, string>

Checks if a string type ends with the given value.

ArgumentTypeDescription
StrstringThe string to check.
MatchstringThe substring to check for at end.
import type {  } from "@halvaradop/ts-utility-types"

type EndsWithBc = <"abc", "bc">
type EndsWithBc = true
type EndsWithAc = <"abc", "ac">
type EndsWithAc = false
type EndsWithWorld = <"hello world", "world">
type EndsWithWorld = true

LengthOfString<Str, string>

Returns the length of a string type as a numeric literal type.

ArgumentTypeDescription
StrstringThe string to get the length of.
import type {  } from "@halvaradop/ts-utility-types"

type Length3 = <"foo">
type Length3 = 3
type Length6 = <"foobar">
type Length6 = 6
type EmptyLength = <"">
type EmptyLength = 0

IndexOfString<Str, string, Match, string>

Returns the index of the first occurrence of a character in the string. Returns -1 if not found.

ArgumentTypeDescription
StrstringThe string to search in.
MatchstringThe character to search for.
import type {  } from "@halvaradop/ts-utility-types"

type IndexO = <"foo", "o">
type IndexO = 1
type NotFound = <"foo", "z">
type NotFound = -1
type FirstA = <"banana", "a">
type FirstA = 1