Try it yourself with our free Url Encoder tool — runs entirely in your browser, no signup needed.

How to URL decode in TypeScript

How to URL decode in TypeScript

URL decoding is the process of converting a URL-encoded string back to its original form. In TypeScript, URL decoding is an essential operation when working with web applications, APIs, or any scenario where data is transmitted as URLs. URL encoding replaces special characters with a % symbol followed by their ASCII code in hexadecimal, which can make it difficult for humans to read and process. Decoding these URLs is crucial for extracting and using the original data. In this article, we'll explore how to URL decode in TypeScript, covering a quick example, a step-by-step breakdown, handling edge cases, common mistakes, performance tips, and frequently asked questions.

Quick Example

Here's a minimal example of URL decoding in TypeScript using the decodeURIComponent function:

function urlDecode(encodedUrl: string): string {
  try {
    return decodeURIComponent(encodedUrl);
  } catch (error) {
    console.error(`Error decoding URL: ${error}`);
    return encodedUrl;
  }
}

const encodedUrl = "https://example.com/path%20with%20spaces";
const decodedUrl = urlDecode(encodedUrl);
console.log(decodedUrl); // Output: https://example.com/path with spaces

This example defines a urlDecode function that takes an encoded URL string as input and returns the decoded URL string. The decodeURIComponent function is used to perform the decoding, and any errors are caught and logged to the console.

Step-by-Step Breakdown

Let's walk through the urlDecode function line by line:

  1. function urlDecode(encodedUrl: string): string {
    • This line defines the urlDecode function, which takes a single argument encodedUrl of type string and returns a string value.
  2. try {
    • This line begins a try-catch block to handle any errors that may occur during the decoding process.
  3. return decodeURIComponent(encodedUrl);
    • This line uses the decodeURIComponent function to decode the input encodedUrl string. decodeURIComponent is a built-in TypeScript function that replaces %xx escapes with their corresponding characters.
  4. } catch (error) {
    • This line catches any errors that occur during the decoding process.
  5. console.error(Error decoding URL: ${error});
    • This line logs an error message to the console if an error occurs.
  6. return encodedUrl;
    • This line returns the original encoded URL string if an error occurs during decoding.

Handling Edge Cases

Here are some common edge cases to consider when URL decoding in TypeScript:

Empty/Null Input

const encodedUrl = "";
const decodedUrl = urlDecode(encodedUrl);
console.log(decodedUrl); // Output: ""

In this case, the decodeURIComponent function will return an empty string, which is the expected behavior.

Invalid Input

const encodedUrl = "% invalid %";
const decodedUrl = urlDecode(encodedUrl);
console.log(decodedUrl); // Output: % invalid %

In this case, the decodeURIComponent function will throw a URIError exception, which is caught and logged to the console.

Large Input

const encodedUrl = "https://example.com/very/long/path%20with%20many%20spaces";
const decodedUrl = urlDecode(encodedUrl);
console.log(decodedUrl); // Output: https://example.com/very/long/path with many spaces

In this case, the decodeURIComponent function can handle large input strings without issues.

Unicode/Special Characters

const encodedUrl = "https://example.com/path%20with%20unicode%20chars%F0%9F%98%80";
const decodedUrl = urlDecode(encodedUrl);
console.log(decodedUrl); // Output: https://example.com/path with unicode chars 

In this case, the decodeURIComponent function correctly decodes the Unicode characters.

Common Mistakes

Here are three common mistakes developers make when URL decoding in TypeScript:

1. Not handling errors

// Wrong code
function urlDecode(encodedUrl: string): string {
  return decodeURIComponent(encodedUrl);
}

// Corrected code
function urlDecode(encodedUrl: string): string {
  try {
    return decodeURIComponent(encodedUrl);
  } catch (error) {
    console.error(`Error decoding URL: ${error}`);
    return encodedUrl;
  }
}

2. Not checking for null/empty input

// Wrong code
function urlDecode(encodedUrl: string): string {
  if (encodedUrl) {
    return decodeURIComponent(encodedUrl);
  }
}

// Corrected code
function urlDecode(encodedUrl: string): string {
  if (!encodedUrl) {
    return "";
  }
  try {
    return decodeURIComponent(encodedUrl);
  } catch (error) {
    console.error(`Error decoding URL: ${error}`);
    return encodedUrl;
  }
}

3. Not using the correct decoding function

// Wrong code
function urlDecode(encodedUrl: string): string {
  return decodeURI(encodedUrl);
}

// Corrected code
function urlDecode(encodedUrl: string): string {
  return decodeURIComponent(encodedUrl);
}

Performance Tips

Here are three practical performance tips for URL decoding in TypeScript:

1. Use decodeURIComponent instead of decodeURI

decodeURIComponent is more efficient than decodeURI because it only decodes the URL components, whereas decodeURI decodes the entire URL.

2. Avoid unnecessary decoding

Only decode the URL components that need to be decoded. Decoding the entire URL can be unnecessary and inefficient.

3. Use caching

If you need to decode the same URL multiple times, consider caching the decoded result to avoid redundant decoding operations.

FAQ

Q: What is the difference between decodeURI and decodeURIComponent?

A: decodeURI decodes the entire URL, while decodeURIComponent decodes only the URL components.

Q: How do I handle errors during URL decoding?

A: Use a try-catch block to catch and handle any errors that occur during the decoding process.

Q: Can I use decodeURIComponent to decode Unicode characters?

A: Yes, decodeURIComponent correctly decodes Unicode characters.

Q: How do I optimize URL decoding performance?

A: Use decodeURIComponent instead of decodeURI, avoid unnecessary decoding, and consider caching decoded results.

Q: What happens if I pass an empty string to decodeURIComponent?

A: decodeURIComponent returns an empty string if the input is an empty string.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp