bugger.
TYPESCRIPT / PRACTICAL GUIDE

TypeScript Debugging: Runtime Type & Origin Bugs

Strict types eliminate whole classes of typos, but real TypeScript production bugs live in runtime boundaries: URL parsing, authentication configuration defaults, Map vs plain-object keys, and async cache timing. Work through the patterns below and test your fixes in isolated TypeScript challenges.

Why do TypeScript programs fail even when `tsc --strict` passes?

TypeScript checks compile-time types, not runtime trust boundaries, async timing, or external inputs. Passing type checks cannot stop a URL from resolving to another origin, an empty allowlist from trusting unsigned tokens, or concurrent awaits from duplicating work.

  • Resolving `new URL(input, base)` can switch hosts via absolute URLs or protocol-relative `//evil.test` references.
  • Falsy checks (`x || default`) and optional-property checks (`obj[key] !== undefined`) conflate missing keys with explicit values.
  • Async deduplication caches that `await` before storing an in-flight promise allow concurrent callers to trigger duplicate requests.
  • Synchronous lifecycle callbacks can re-enter and mutate reference counts before the outer function finishes registering.

1. Validate the resolved URL origin, not the input string

Calling `new URL(path, "https://api.example/v1/")` accepts absolute URLs and protocol-relative strings like `//attacker.example/steal`, silently replacing the base host. Checking `startsWith(base)` can still fail on lookalike domains such as `https://api.example.attacker.test`.

Resolve the target URL first, verify its scheme is `http:` or `https:`, reject embedded credentials, and compare `target.origin === base.origin` before attaching any API key or Authorization header.

TypeScript example
const base = new URL("https://api.example/v1/");
const target = new URL(userPath, base);
if (target.origin !== base.origin || target.username || target.password) {
  throw new Error("Cross-origin or credentialed target blocked");
}

2. Publish in-flight promises synchronously

If an async cache helper checks `cache.get(key)`, awaits a setup step, and only then calls `cache.set(key, promise)`, two concurrent callers arriving before the first await both miss the cache and start duplicate work.

Create and store the in-flight promise synchronously before yielding, and evict failed promises from the cache so later callers can retry.

3. Separate absent properties from undefined values

In TypeScript, `{ a: undefined }` satisfies `{ a?: number }`, yet `Object.hasOwn(obj, "a")` is true while `"a" in obj` differs from reading `obj.a`. Similarly, checking truthiness on a Map key or parsed tuple element drops valid `0`, `""`, or `false` entries.

Test presence explicitly with `Object.hasOwn` or `Map.prototype.has` whenever missing state and explicit empty state carry different meanings.

  • Verify `target.origin === base.origin` after `new URL(input, base)`.
  • Store in-flight promises synchronously before any `await`.
  • Require explicit opt-in config before accepting unsigned tokens.

Frequently asked questions

How can `new URL(path, base)` bypass a base URL in TypeScript or JavaScript?
According to the WHATWG URL standard, if `path` is an absolute URL (https://other.test) or protocol-relative URL (//other.test), `new URL(path, base)` discards `base` host and resolves to the new origin. Always check `target.origin === base.origin` after resolution.
Why does `tsc --strict` not catch async cache stampedes or reentrancy bugs?
Type checking verifies static shapes, not execution order across microtasks or synchronous callbacks. State ownership and promise publication timing must be verified with concurrency and lifecycle regression tests.

Further reading

Start with primary reports and documentation. Community discussions are discovery leads, not evidence on their own.

PUT THE METHOD TO WORK

Practice on a focused challenge.

Explore TypeScript challenges
BUILD A REGULAR PRACTICE

Start free. Keep following the evidence.

Explore the current challenge collection, or see what is planned for future practice packs.