• _hello
  • _about-me
  • _projects
  • _blog
  • _support
_contact-me
find me in:
@faeztgh
cd ../blog~/blog/decoding-url-parameters-typescript
  • TypeScript
  • Type Inference
  • Template Literal Types
  • URL Parameters

Decoding URL Parameters with TypeScript: An In-Depth Guide

Learn how TypeScript's advanced types and ts-toolbelt can generate precise IntelliSense auto-completion for URL query parameters at compile time.

FFaez Tgh·Dec 19, 20232 min read·352 words
// share: post linkedin
Decoding URL query parameters with TypeScript advanced types and ts-toolbelt

Welcome to a deep dive into an impressive demonstration of TypeScript's advanced types and the utility library, ts-toolbelt. The objective of this article is to explore a TypeScript script that generates IntelliSense auto-completion for URL query parameters. Let's break down each code segment to understand its function better.

Importing Utilities#

ts
import { String, Union } from "ts-toolbelt";

The initial line imports utility functions from the ts-toolbelt package. This library offers valuable utilities to harness TypeScript's potential powerfully, thereby providing solutions to develop more robust and dynamic scripts.

Defining URL Query#

ts
const query = /home?a=foo&b=bar;
type Query = typeof query;

The script begins by defining a string, query, representing our URL. It then employs TypeScript's typeof operator to derive a type from query. Essentially, Query becomes a type related to the content stored in query.

Extracting Query Parameters#

ts
type SecondaryQueryPart = String.Split<Query, "?">[1];

In this particular section, the String.Split function of ts-toolbelt splits the URL at the "?" character, generating an array of substrings. Index [1] retrieves the substring following "?", which embodies the actual query parameters.

Splitting Individual Parameters#

ts
type QueryElements = String.Split<SecondaryQueryPart, "&">;

Taking things further, the resultant query string undergoes another split operation using the "&" character. Consequently, it isolates individual query parameters, returning them as an array of strings.

Creating Query Parameter Type Map#

ts
type QueryParams = {
    [QueryElement in QueryElements[number]]: {
        [key in String.Split<QueryElement, "=">[0]]: String.Split<
            QueryElement,
            "="
        >[1];
    };
}[QueryElements[number]];

This intricate segment constructs a new type, QueryParams, representing key-value pairs in the query string. It cycles through each QueryElement from the QueryElements type, splits it around "=", and forms a new record type with these keys and corresponding values.

In essence, it's creating a map of parameter types, thereby enabling subsequent code to understand what type each parameter should be.

Building Final Object#

ts
const obj: Union.Merge = {
    a: "foo",
    b: "bar",
};

Lastly, an object obj is defined with a type Union.Merge, another utility from ts-toolbelt. This function merges intersecting types into a single type. Therefore, obj epitomizes an object with inferred types from specific URL query parameters, offering intelligent auto-complete suggestions as we manipulate it.

Conclusion#

In summary, this TypeScript script beautifully demonstrates how we can create a method to infer keys and potential values for URL parameters from given URLs, enriching the typing experience in TypeScript. Such smart usage of TypeScript's advanced types and utility libraries like ts-toolbelt manifests the power of modern JavaScript ecosystems in building complex, but easily maintainable software architecture.

Source: typescript-101/Advanced/day7

#on this page

  • Importing Utilities
  • Defining URL Query
  • Extracting Query Parameters
  • Splitting Individual Parameters
  • Creating Query Parameter Type Map
  • Building Final Object
  • Conclusion
Leveraging Union Types and Union Autocomplete Helper in TypeScript olderAn In-Depth Guide to Key Removal in TypeScriptnewer

# related-posts

3 more
  • 01
    Getting Structured Output From LLMs Without the GuessworkJul 7, 2026·5 min
  • 02
    Discriminated Unions: TypeScript's Best Modelling ToolJul 4, 2026·4 min
  • 03
    Model Context Protocol (MCP), Explained for Web DevelopersJun 28, 2026·6 min