string-utility-types
raw JSON → 2.1.0 verified Mon Apr 27 auth: no javascript
A TypeScript utility library providing advanced string type manipulations via template literal types. Version 2.1.0 offers types like Split, Join, ReplaceAt, and path matching utilities (MatchesPathPattern, MatchesPathPatternLax) for compile-time string processing. Released under MIT, it is actively maintained with a focus on type safety for routing and string operations. Unlike similar libraries, it provides conservative path pattern matching to avoid false negatives, with explicit documentation of limitations. Recommended for TypeScript projects needing robust string type inference.
Common errors
error Type 'Split<...>' does not satisfy the constraint 'string[]'. ↓
cause Using Split on a non-string type or with invalid delimiter.
fix
Ensure S and Delimiter are string literals, e.g., Split<'a/b', '/'>
error Type instantiation is excessively deep and possibly infinite. ↓
cause Recursive type evaluation on very long strings or deep arrays.
fix
Limit string length or use simpler types; avoid nesting.
error Cannot find name 'Split'. Did you mean 'split'? ↓
cause Using default import instead of named import.
fix
Use 'import { Split } from 'string-utility-types'.
Warnings
gotcha MatchesPathPattern is conservative and may return true for paths that technically could match due to string interpolation (e.g., `${string}` could be `/123/author`). ↓
fix Use MatchesPathPatternLax if you can guarantee no slashes in interpolations.
gotcha MatchesPathPattern only works up to 6 path segments. Beyond that, behavior is undefined. ↓
fix Break down path patterns or use a different type for longer paths.
breaking Version 2.0.0 removed deprecated types from v1 (e.g., older Split/Join implementations) – updates may break code relying on old signatures. ↓
fix Update named imports to current API; see migration guide.
Install
npm install string-utility-types yarn add string-utility-types pnpm add string-utility-types Imports
- Split wrong
import Split from 'string-utility-types'correctimport { Split } from 'string-utility-types' - Join wrong
import Join from 'string-utility-types'correctimport { Join } from 'string-utility-types' - MatchesPathPattern
import { MatchesPathPattern } from 'string-utility-types'
Quickstart
import { Split, Join, MatchesPathPattern, MatchesPathPatternLax } from 'string-utility-types';
// Example usage
type Path = 'home/user/docs';
type Parts = Split<Path, '/'>; // ['home', 'user', 'docs']
type Rejoined = Join<Parts, '-'>; // 'home-user-docs'
type IsMatch = MatchesPathPattern<'/users/123', '/users/:id'>; // true
type IsMatchLax = MatchesPathPatternLax<'/users/123', '/users/:id'>; // true
// Note: MatchesPathPattern may return true for interpolated strings
// e.g., MatchesPathPattern<"/users/${string}", '/users/:id'> is true
// Use MatchesPathPatternLax if you know interpolations have no slashes.