
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
string-utility-types
Advanced tools
Utility typescript types for strings
npm install string-utility-types
Split<S, Delimiter>Splits a string into an array of strings, using a delimiter.
Type Parameters:
S extends string - The string to splitDelimiter extends string - The delimiter to split onExample:
type Result = Split<'a/b/c', '/'> // ['a', 'b', 'c']
type Empty = Split<'', '/'> // []
type Single = Split<'a', '/'> // ['a']
Join<Parts, Delimiter>Joins an array of strings into a string, using a delimiter.
Type Parameters:
Parts extends string[] - The array of strings to joinDelimiter extends string - The delimiter to join withExample:
type Result = Join<['a', 'b', 'c'], '/'> // 'a/b/c'
type Empty = Join<[], '/'> // ''
type Single = Join<['a'], '/'> // 'a'
ReplaceAt<Arr, Index, New>Replaces the element at the given index in an array with a new value.
Type Parameters:
Arr extends any[] - The array to modifyIndex extends number - The index at which to replace the elementNew - The new value to place at the indexExample:
type Result = ReplaceAt<['a', 'b', 'c'], 1, 'd'> // ['a', 'd', 'c']
type First = ReplaceAt<['a', 'b', 'c'], 0, 'x'> // ['x', 'b', 'c']
MatchesPathPattern<TPath, TPattern>Checks if a path may match a given path pattern. This method is conservative and may return true for more paths than you're expecting.
Type Parameters:
TPath extends string - The path to checkTPattern extends string - The path pattern to match against (e.g., /books/:id)Important Notes:
true for paths that technically could match due to string interpolationMatchesPathPattern</books/${string}, '/books/:id/author'> returns true because ${string} could be /123/author, which would match the patternMatchesPathPatternLax insteadExample:
// Exact matches
type Match1 = MatchesPathPattern<'/books/123', '/books/:id'> // true
type Match2 = MatchesPathPattern<'/books', '/books'> // true
// Conservative matches (may be true even if not exact)
type Match3 = MatchesPathPattern<`/books/${string}`, '/books/:id/author'> // true
type Match4 = MatchesPathPattern<string, '/books/:id'> // true
// Non-matches
type NoMatch1 = MatchesPathPattern<'', '/books'> // false
type NoMatch2 = MatchesPathPattern<'/cars', '/books'> // false
type NoMatch3 = MatchesPathPattern<'/books/123/extra', '/books/:id'> // false
MatchesPathPatternLax<TPath, TPattern>Similar to MatchesPathPattern, but assumes that all string interpolations are valid path segments and have no slashes. By making this assumption (hence "Lax"), it can provide narrower matching than MatchesPathPattern.
Type Parameters:
TPath extends string - The path to checkTPattern extends string - The path pattern to match againstImportant Notes:
MatchesPathPattern, this will return false for cases where string interpolation could theoretically match but contains slashesExample:
// Exact matches
type Match1 = MatchesPathPatternLax<'/123', '/:id'> // true
type Match2 = MatchesPathPatternLax<'/books/123', '/books/:id'> // true
type Match3 = MatchesPathPatternLax<'/books/123/authors', '/books/:id/authors'> // true
// Non-matches
type NoMatch1 = MatchesPathPatternLax<'/', '/:id'> // false
type NoMatch2 = MatchesPathPatternLax<'/123/', '/:id'> // false
type NoMatch3 = MatchesPathPatternLax<'/123/authors', '/:id'> // false
type NoMatch4 = MatchesPathPatternLax<'/books', '/books/:id'> // false
type NoMatch5 = MatchesPathPatternLax<'/books/123/extra', '/books/:id'> // false
GetMatchingPathPattern<TPath, TPatterns>Given a path and a union of path patterns, returns the set of patterns that may match the path. This method is conservative and may return more patterns than you're expecting.
Type Parameters:
TPath extends string - The path to checkTPatterns extends string - A union of path patterns to match againstImportant Notes:
GetMatchingPathPattern</books/${typeof myId}, '/books/:id' | '/books/:id/author'> may return both patterns because myId could theoretically be /123/authorsGetMatchingPathPatternLax insteadExample:
type Result = GetMatchingPathPattern<`/books/${string}`, '/books/:id' | '/books/:id/author'>
// Result: '/books/:id' | '/books/:id/author'
GetMatchingPathPatternLax<TPath, TPatterns>Similar to GetMatchingPathPattern, but assumes that all string interpolations in the path do not have any slashes. By making this assumption (hence "Lax"), it can provide narrower matching than GetMatchingPathPattern.
Type Parameters:
TPath extends string - The path to checkTPatterns extends string - A union of path patterns to match againstImportant Notes:
GetMatchingPathPatternLax</books/${typeof myId}, '/books/:id' | '/books/:id/author'> will only return /books/:id because it assumes myId has no slashesExample:
type Result = GetMatchingPathPatternLax<`/books/${string}`, '/books/:id' | '/books/:id/author'>
// Result: '/books/:id'
PathPatternToTemplateLiteral<TPattern>Converts a path pattern to a template literal by replacing placeholders (e.g., :id) with ${string}.
Type Parameters:
TPattern extends string - The path pattern to convertImportant Notes:
/books/${string} would allow paths like /books/123/author, while /books/:id would notNoExtraPathSegmentsExample:
type Result = PathPatternToTemplateLiteral<'/books/:id'> // '/books/${string}'
type Result2 = PathPatternToTemplateLiteral<'/books/:id/authors/:id'> // '/books/${string}/authors/${string}'
NoExtraPathSegments<TPath, TPatterns>Returns never if the path has any extra path segments after matching any of the template literals derived from the patterns, otherwise returns the path.
Type Parameters:
TPath extends string - The path to validateTPatterns extends string - A union of path patterns to check againstExample:
// Valid paths (no extra segments)
type Valid1 = NoExtraPathSegments<'/books/123', '/books/:id'> // '/books/123'
type Valid2 = NoExtraPathSegments<'/books/123/author', '/books/:id/author'> // '/books/123/author'
// Invalid paths (extra segments)
type Invalid1 = NoExtraPathSegments<'/books/123/author', '/books/:id'> // never
type Invalid2 = NoExtraPathSegments<'/books/123/extra', '/books/:id'> // never
PathPatternParams<TPattern>Extracts placeholder parameters (e.g., :id, :userId) from a path pattern and constructs an object type whose keys are the parameter names and whose values are string. Supports multiple parameters.
Type Parameters:
TPattern extends string — The path pattern (e.g., '/books/:id/authors/:userId')Behavior:
:id) are extracted as object keys with value type string.Example:
type Params1 = PathPatternParams<'/books/:id'> // { id: string }
type Params2 = PathPatternParams<'/books/:bookId/authors/:id'> // { bookId: string; id: string }
type Params3 = PathPatternParams<'/static/path'> // {}
FAQs
Utility typescript types for strings
We found that string-utility-types demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.